Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditions in MySQL GROUP_CONCAT

I have a MySQL table with requests

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

and a table with votes on such requests.

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

The kind of view I want:

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

So far, however, I've only been able to accomplish this:

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

The query I used:

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;

So my question is, how do I get 2 group_concats in the same row with different values?

Thanks a lot!

like image 308
Rapsey Avatar asked Mar 23 '12 17:03

Rapsey


People also ask

What does Group_concat do in MySQL?

The GROUP_CONCAT() function in MySQL is used to concatenate data from multiple rows into one field. This is an aggregate (GROUP BY) function which returns a String value, if the group contains at least one non-NULL value. Otherwise, it returns NULL.

What is the difference between concat and Group_concat in MySQL?

The difference here is while CONCAT is used to combine values across columns, GROUP_CONCAT gives you the capability to combine values across rows. It's also important to note that both GROUP_CONCAT and CONCAT can be combined to return desired results.

Is there a limit to Group_concat?

The GROUP_CONCAT() function has a default length of 1024 characters, which is controlled by the global variable group_concat_max_len . If the joined values length is greater than the group_concat_max_len value, then the result string will be truncated.


2 Answers

Try this:

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

Result:

+--------+-------------+---------------------+--------------+
| REQ_ID |  REQ_NAME   |     APPROVEDBY      |  REJECTEDBY  |
+--------+-------------+---------------------+--------------+
|      1 | testrequest | User1, User2, User3 | User4, User5 |
+--------+-------------+---------------------+--------------+
like image 144
Mosty Mostacho Avatar answered Oct 07 '22 00:10

Mosty Mostacho


I tried re-using your query inside another query

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
like image 36
Chetter Hummin Avatar answered Oct 07 '22 02:10

Chetter Hummin