1

I have to add two or more strings from MySQL database, for this I used CONCAT() function.

Here is the first table classes which stores PHP classes.

class_id  class_name
--------  ----------
       1  accountant
       2  attendance

Another table methods which stores each class methods.

class_id  method_name            
--------  -----------------------
       1  __construct            
       1  add_expenses

       2  __construct            
       2  attendance_report

And I write the query for concatenation.

SELECT 
  `cc`.`class_id`,
  `cc`.`class_name`,
  CONCAT(`cm`.`method_name`, ',') AS `method_name` 
FROM
  `classes` AS `cc` 
  LEFT JOIN `methods` AS `cm` 
    ON `cm`.`class_id` = `cc`.`class_id` 
GROUP BY `cc`.`class_name`;

Which is not working. My expected output is

class_id  class_name      method_name 
--------  --------------  ------------
       1  accountant      __construct, add_expenses, .... n
       2  attendance      __construct, attendance_report, .... n

Any ideas?

1 Answer 1

3

Use

GROUP_CONCAT

instead of

CONCAT

GROUP_CONCAT(cm.method_name) you do not needs to pass comma as separator that will be taken default.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.