Mr.Happy
Mr.Happy

Reputation: 2647

How to concat find_in_set results with comma separated string in mysql

I have used FIND_IN_SET to my below select sql query and got the result but I want to store industrie_branch comma separated list

Select Query:

SELECT i.instagrammer_id
      , CONCAT(i.first_name,' ',i.last_name) AS instagrammer_name
      , FLOOR(DATEDIFF (NOW(), i.date_of_birth)/365) AS age
      , i.date_of_birth
      , i.gender
      , i.email
      , i.created_date
      , b.name_en AS industrie_branch
FROM instagrammer AS i
LEFT JOIN industries_branches b ON FIND_IN_SET(b.industrie_branch_id, i.industrie_branch)
WHERE i.status = 1
ORDER BY instagrammer_name DESC

and getting this output of above query:

instagrammer_id     instagrammer_name       age     date_of_birth       gender      email               industrie_branch
***************     *****************       ***     *************       ******      ****************    ****************
37                  Jack                    27      1988-09-30          male        [email protected]    Food
37                  Jack                    27      1988-09-30          male        [email protected]    Photography
38                  Tom                     27      1988-09-30          male        [email protected]       Lifestyle
38                  Tom                     27      1988-09-30          male        [email protected]       Luxury

but I want this output with comma separated list for industrie_branch:

instagrammer_id     instagrammer_name       age     date_of_birth       gender      email               industrie_branch
***************     *****************       ***     *************       ******      ****************    ****************
37                  Jack                    27      1988-09-30          male        [email protected]    Food, Photography
38                  Tom                     27      1988-09-30          male        [email protected]       Lifestyle, Luxury

Upvotes: 2

Views: 1082

Answers (1)

Arun Krish
Arun Krish

Reputation: 2153

use it like this with GROUP_CONCAT

SELECT i.instagrammer_id
  , CONCAT(i.first_name,' ',i.last_name) AS instagrammer_name
  , FLOOR(DATEDIFF (NOW(), i.date_of_birth)/365) AS age
  , i.date_of_birth
  , i.gender
  , i.email
  , i.created_date
  , GROUP_CONCAT(b.name_en) AS industrie_branch
FROM instagrammer AS i
LEFT JOIN industries_branches b ON FIND_IN_SET(b.industrie_branch_id, i.industrie_branch)
WHERE i.status = 1
GROUP BY i.instagrammer_id
ORDER BY instagrammer_name DESC

Upvotes: 1

Related Questions