合并结果并用逗号分隔

时间:2012-12-26 06:36:07

标签: mysql sql

我有这个问题:

SELECT
  tud.detail_company_name AS company_name,
  tud.detail_country AS company_country,
  trv.review_title AS review_title
FROM users_detail tud
INNER JOIN users_auth tua
  ON tud.user_id = tua.user_id
LEFT JOIN reviews trv
  ON tud.user_id = trv.review_company_id
WHERE
  tua.auth_user_type = "company" OR tua.auth_user_type = "guestcompany"

查询工作正常,它返回如下结果:

Array
(
    [0] => Array
        (
            [company_name] => The Coffee Brewery
            [company_country] => US
            [review_title] => 
        )
    [1] => Array
        (
            [company_name] => Crea Nail Art Studio
            [company_country] => SG
            [review_title] => 
        )
    [2] => Array
        (
            [company_name] => Hello Mall
            [company_country] => JP
            [review_title] => Fake goods!
        )
    [3] => Array
        (
            [company_name] => Hello Mall
            [company_country] => JP
            [review_title] => Never buy in there!
        )
)

你注意到数组键2& 3具有相同的 company_name 但不同的 review_title 。是否有可能必须在一个结果中加入并用逗号分隔?比如说这个结果:

Array
(
    [0] => Array
        (
            [company_name] => The Coffee Brewery
            [company_country] => US
            [review_title] => 
        )
    [1] => Array
        (
            [company_name] => Crea Nail Art Studio
            [company_country] => SG
            [review_title] => 
        )
    [2] => Array
        (
            [company_name] => Hello Mall
            [company_country] => JP
            [review_title] => Fake goods!, Never buy in there!
        )
)

修改

如果答案如此:

Array
(
    [0] => Array
        (
            [company_name] => The Coffee Brewery
            [company_country] => US
            [review_title] => 
            [review_approved] =>
        )
    [1] => Array
        (
            [company_name] => Crea Nail Art Studio
            [company_country] => SG
            [review_title] => 
            [review_approved] =>
        )
    [2] => Array
        (
            [company_name] => Hello Mall
            [company_country] => JP
            [review_title] => Fake goods!
            [review_approved] => 1
        )
    [3] => Array
        (
            [company_name] => Hello Mall
            [company_country] => JP
            [review_title] => Never buy in there!
            [review_approved] => 0
        )
)

如何在 review_title GROUP_CONCAT上添加 0 review_approved ?所以它会这样输出?

Array
(
    [0] => Array
        (
            [company_name] => The Coffee Brewery
            [company_country] => US
            [review_title] => 
        )
    [1] => Array
        (
            [company_name] => Crea Nail Art Studio
            [company_country] => SG
            [review_title] => 
        )
    [2] => Array
        (
            [company_name] => Hello Mall
            [company_country] => JP
            [review_title] => Fake goods!
        )
)

1 个答案:

答案 0 :(得分:2)

是。使用GROUP_CONCAT()

SELECT
  tud.detail_company_name AS company_name,
  tud.detail_country AS company_country,
  GROUP_CONCAT(trv.review_title SEPARATOR ',') AS review_titles
FROM users_detail tud
INNER JOIN users_auth tua
  ON tud.user_id = tua.user_id
LEFT JOIN reviews trv
  ON tud.user_id = trv.review_company_id
WHERE
  tua.auth_user_type = :company OR tua.auth_user_type = :guestcompany
GROUP BY 
    tud.detail_company_name,
    tud.detail_country;