我有一个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;
我的问题是,如何在同一行中获取两个不同值的group_concat?
非常感谢!