MySQL GROUP_CONCAT 中的条件

Posted

技术标签:

【中文标题】MySQL GROUP_CONCAT 中的条件【英文标题】:Conditions in MySQL GROUP_CONCAT 【发布时间】:2012-04-08 06:58:03 【问题描述】:

我有一个带有请求的 mysql

+--------+-------------+-----+
| req_id | req_name    | ... |
+--------+-------------+-----+
| 1      | testrequest | ... |
+--------+-------------+-----+

以及对此类请求进行投票的表格。

+--------+-----------+----------+
| req_id | vote_name | approved |
+--------+-----------+----------+
| 1      | User1     | 1        |
| 1      | User2     | 1        |
| 1      | User3     | 1        |
| 1      | User4     | 0        |
| 1      | User5     | 0        |
+--------+-----------+----------+

我想要的那种视图:

+--------+-------------+---------------------+--------------+
| req_id | req_name    | approved_by         | rejected_by  |
+--------+-------------+---------------------+--------------+
| 1      | testrequest | User1, User2, User3 | User4, User5 |
+--------+-------------+---------------------+--------------+

然而,到目前为止,我只能做到这一点:

+--------+-------------+----------+---------------------+
| req_id | req_name    | approved | by                  |
+--------+-------------+----------+---------------------+
| 1      | testrequest | YES      | User1, User2, User3 |
| 1      | testrequest | NO       | User4, User5        |
+--------+-------------+----------+---------------------+

我使用的查询:

SELECT requests.req_id, req_name, CASE
        WHEN approved THEN 'YES'
        ELSE 'NO'
        END AS approved, GROUP_CONCAT(vote_name ORDER BY vote_name ASC SEPARATOR ', ') AS by
FROM requests
LEFT JOIN votes ON requests.req_id = votes.req_id
GROUP BY requests.req_id, approved
ORDER BY requests.req_id DESC;

所以我的问题是,如何在同一行中获得 2 个具有不同值的 group_concats?

非常感谢!

【问题讨论】:

Order by 正在与 group_concat 合作 【参考方案1】:

试试这个:

select r.req_id, r.req_name,
  group_concat(if(approved, vote_name, null) separator ', ') approvedBy,
  group_concat(if(approved, null, vote_name) separator ', ') rejectedBy
from requests r
left join votes v on r.req_id = v.req_id

结果:

+--------+-------------+---------------------+--------------+
| REQ_ID |  REQ_NAME   |     APPROVEDBY      |  REJECTEDBY  |
+--------+-------------+---------------------+--------------+
|      1 | testrequest | User1, User2, User3 | User4, User5 |
+--------+-------------+---------------------+--------------+

【讨论】:

【参考方案2】:

我尝试在另一个查询中重复使用您的查询

SELECT req_id,
   req_name,
   GROUP_CONCAT(case approved when 'YES' then voted_by else null end SEPARATOR ', ') AS approved_by,
   GROUP_CONCAT(case approved when 'NO' then voted_by else null end SEPARATOR ', ') AS rejected_by
FROM
(
SELECT requests.req_id, req_name, CASE
    WHEN approved THEN 'YES'
    ELSE 'NO'
    END AS approved, 
    GROUP_CONCAT(vote_name ORDER BY vote_name ASC SEPARATOR ', ') AS voted_by
FROM requests
LEFT JOIN votes ON requests.req_id = votes.req_id
GROUP BY requests.req_id, approved
ORDER BY requests.req_id DESC
) t
group by req_id

【讨论】:

感谢您的快速回复!您认为不使用子查询就可以实现这一目标吗?因为如果我没记错的话,MySQL 不允许视图的 FROM 子句中的子查询.. 嗯,好的。将不得不调查它。

以上是关于MySQL GROUP_CONCAT 中的条件的主要内容,如果未能解决你的问题,请参考以下文章

Mysql count group_concat 高级用法(count 过滤条件,group_concat过滤条件)

MySQL:GROUP_CONCAT 结果的条件?

Mysql group_concat with distinct and where 给出奇怪的结果

MySQL 触发器嵌套 Group_Concat 重复

如何仅使用“group_concat”过滤表 mysql 中的行

mysql根据不同查询条件查询