如何在mysql

时间:2015-11-24 08:07:25

标签: mysql sql database

我在下面的select sql查询中使用了FIND_IN_SET得到了结果,但我想存储industrie_branch逗号分隔列表

选择查询:

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

获取上述查询的输出:

instagrammer_id     instagrammer_name       age     date_of_birth       gender      email               industrie_branch
***************     *****************       ***     *************       ******      ****************    ****************
37                  Jack                    27      1988-09-30          male        jack@hotmail.com    Food
37                  Jack                    27      1988-09-30          male        jack@hotmail.com    Photography
38                  Tom                     27      1988-09-30          male        tom@gmail.com       Lifestyle
38                  Tom                     27      1988-09-30          male        tom@gmail.com       Luxury

但是我希望这个输出以逗号分隔列表industrie_branch

instagrammer_id     instagrammer_name       age     date_of_birth       gender      email               industrie_branch
***************     *****************       ***     *************       ******      ****************    ****************
37                  Jack                    27      1988-09-30          male        jack@hotmail.com    Food, Photography
38                  Tom                     27      1988-09-30          male        tom@gmail.com       Lifestyle, Luxury

1 个答案:

答案 0 :(得分:1)

使用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