没有条件的 Group Concat
Group Concat without conditionals
我正在尝试重写我的查询以使用列而不是行。我有这个查询:
mysql> SELECT r.id as responseId, a.id as answerId, o.id, optionId, q.label as questionLabel, o.label as optionLabel
-> FROM answer a
-> INNER JOIN question q ON questionId = q.id
-> INNER JOIN answer_options_option ao ON a.id = ao.answerId
-> INNER JOIN `option` o ON ao.optionId = o.id
-> INNER JOIN response r ON a.responseId = r.id
-> ORDER BY r.id, a.id, o.id;
这给了我这个输出:
+------------+----------+----+----------+---------------+-------------+
| responseId | answerId | id | optionId | questionLabel | optionLabel |
+------------+----------+----+----------+---------------+-------------+
| 1 | 1 | 2 | 2 | Q1 | no |
| 1 | 2 | 4 | 4 | Q2 | b |
| 2 | 3 | 1 | 1 | Q1 | yes |
| 2 | 4 | 3 | 3 | Q2 | a |
| 2 | 4 | 4 | 4 | Q2 | b |
| 2 | 4 | 5 | 5 | Q2 | c |
+------------+----------+----+----------+---------------+-------------+
但我想得到这个输出:
+------------+-----------+-------+
| responseId | Q1 | Q2 |
+------------+-----------+-------+
| 1 | no | b |
| 2 | yes | a,b,c |
+------------+-----------+-------+
所以我拼凑了这个查询:
mysql> SELECT r.id as responseId,
-> IF(q.label = 'Q1', GROUP_CONCAT(o.label), NULL) as Q1,
-> IF(q.label = 'Q2', GROUP_CONCAT(o.label), NULL) as Q2
-> FROM answer a
-> INNER JOIN question q ON questionId = q.id
-> INNER JOIN answer_options_option ao ON a.id = ao.answerId
-> INNER JOIN `option` o ON ao.optionId = o.id
-> INNER JOIN response r ON a.responseId = r.id
-> GROUP BY r.id;
但它给了我这个输出:
+------------+-----------+------+
| responseId | Q1 | Q2 |
+------------+-----------+------+
| 1 | no,b | NULL |
| 2 | yes,a,b,c | NULL |
+------------+-----------+------+
甚至可以这样使用 GROUP_CONCAT
吗?还有其他方法可以完成我想做的事情吗?
这里是 Fiddle。
您需要将 GROUP_CONCAT
移到 条件之外,以便您根据问题聚合选项,而不是仅仅根据问题获得整个聚合结果值:
SELECT r.id as responseId,
GROUP_CONCAT(CASE WHEN q.label = 'Q1' THEN o.label END ORDER BY o.label) as Q1,
GROUP_CONCAT(CASE WHEN q.label = 'Q2' THEN o.label END ORDER BY o.label) as Q2
FROM answer a
INNER JOIN question q ON questionId = q.id
INNER JOIN answer_options_option ao ON a.id = ao.answerId
INNER JOIN `option` o ON ao.optionId = o.id
INNER JOIN response r ON a.responseId = r.id
GROUP BY r.id;
输出:
responseId Q1 Q2
1 no b
2 yes a,b,c
我正在尝试重写我的查询以使用列而不是行。我有这个查询:
mysql> SELECT r.id as responseId, a.id as answerId, o.id, optionId, q.label as questionLabel, o.label as optionLabel
-> FROM answer a
-> INNER JOIN question q ON questionId = q.id
-> INNER JOIN answer_options_option ao ON a.id = ao.answerId
-> INNER JOIN `option` o ON ao.optionId = o.id
-> INNER JOIN response r ON a.responseId = r.id
-> ORDER BY r.id, a.id, o.id;
这给了我这个输出:
+------------+----------+----+----------+---------------+-------------+
| responseId | answerId | id | optionId | questionLabel | optionLabel |
+------------+----------+----+----------+---------------+-------------+
| 1 | 1 | 2 | 2 | Q1 | no |
| 1 | 2 | 4 | 4 | Q2 | b |
| 2 | 3 | 1 | 1 | Q1 | yes |
| 2 | 4 | 3 | 3 | Q2 | a |
| 2 | 4 | 4 | 4 | Q2 | b |
| 2 | 4 | 5 | 5 | Q2 | c |
+------------+----------+----+----------+---------------+-------------+
但我想得到这个输出:
+------------+-----------+-------+
| responseId | Q1 | Q2 |
+------------+-----------+-------+
| 1 | no | b |
| 2 | yes | a,b,c |
+------------+-----------+-------+
所以我拼凑了这个查询:
mysql> SELECT r.id as responseId,
-> IF(q.label = 'Q1', GROUP_CONCAT(o.label), NULL) as Q1,
-> IF(q.label = 'Q2', GROUP_CONCAT(o.label), NULL) as Q2
-> FROM answer a
-> INNER JOIN question q ON questionId = q.id
-> INNER JOIN answer_options_option ao ON a.id = ao.answerId
-> INNER JOIN `option` o ON ao.optionId = o.id
-> INNER JOIN response r ON a.responseId = r.id
-> GROUP BY r.id;
但它给了我这个输出:
+------------+-----------+------+
| responseId | Q1 | Q2 |
+------------+-----------+------+
| 1 | no,b | NULL |
| 2 | yes,a,b,c | NULL |
+------------+-----------+------+
甚至可以这样使用 GROUP_CONCAT
吗?还有其他方法可以完成我想做的事情吗?
这里是 Fiddle。
您需要将 GROUP_CONCAT
移到 条件之外,以便您根据问题聚合选项,而不是仅仅根据问题获得整个聚合结果值:
SELECT r.id as responseId,
GROUP_CONCAT(CASE WHEN q.label = 'Q1' THEN o.label END ORDER BY o.label) as Q1,
GROUP_CONCAT(CASE WHEN q.label = 'Q2' THEN o.label END ORDER BY o.label) as Q2
FROM answer a
INNER JOIN question q ON questionId = q.id
INNER JOIN answer_options_option ao ON a.id = ao.answerId
INNER JOIN `option` o ON ao.optionId = o.id
INNER JOIN response r ON a.responseId = r.id
GROUP BY r.id;
输出:
responseId Q1 Q2
1 no b
2 yes a,b,c