When using INNER JOIN , Is there any way where I can concat multiple rows data from table 2 into one row of data (comma separated values )

Currently , When I use joins then each row from cust_base has 3 rows of data .(due to 3 values from cust_addition_value table ).
I want multiple rows of data into one single row with comma separated values .

Here is the code :

 $statement = $pdo->prepare("SELECT 
  cust_base.field1,cust_base.field2,cust_addition_value.field3
  FROM cust_base INNER JOIN cust_addition_value on cust_base.id=cust_addition_value.cid");
 $statement->execute();
 $data1 =$statement->fetchAll();

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Use GROUP BY and GROUP_CONCAT

SELECT 
  cust_base.field1,cust_base.field2,GROUP_CONCAT(cust_addition_value.field3)
  FROM cust_base INNER JOIN cust_addition_value on cust_base.id=cust_addition_value.cid
  GROUP BY cust_base.field1,cust_base.field2


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x