使用 GROUP_CONCAT 时,ORDER BY 子句似乎可以正常工作。但是,当使用 ISNULL 或 COALESCE 替换 NULL 值时,尽管实际值是有序的,但 NULL 值都在连接的开头结束(见下文)。如何让我的 NULL 替换值以正确的列顺序排列,以便它们正确地表示被替换的值。
SELECT filledForm.creationDate AS date, GROUP_CONCAT(IFNULL(answers.answer, "NULL") ORDER BY answers.promptID) AS answers
FROM filledForm
JOIN prompts ON filledForm.formID = prompts.formID AND prompts.formID = 100
LEFT JOIN answers ON prompts.promptID = answers.promptID AND filledForm.filledFormID = answers.filledFormID
GROUP BY filledForm.filledFormID;
使用一些示例数据,这给出:
+---------------------+-----------------------------------------------------------------+
| date | answers |
+---------------------+-----------------------------------------------------------------+
| 2023-03-10 14:21:03 | NULL,NULL,NULL,NULL,NULL,bob,2023-03-16,English |
| 2023-03-11 02:28:23 | NULL,NULL,NULL,Johnny Depp,60,[email protected],2023-03-24,German |
| 2023-03-11 02:32:41 | NULL,NULL,Tom,22,[email protected],2023-03-29,11:00,English |
+---------------------+-----------------------------------------------------------------+
代替:
+---------------------+-----------------------------------------------------------------+
| date | answers |
+---------------------+-----------------------------------------------------------------+
| 2023-03-10 14:21:03 | bob,NULL,NULL,2023-03-16,NULL,English,NULL,NULL |
| 2023-03-11 02:28:23 | Johnny Depp,60,[email protected],2023-03-24,NULL,German,NULL,NULL |
| 2023-03-11 02:32:41 | Tom,22,[email protected],2023-03-29,11:00,EnglishNULL,NULL |
+---------------------+-----------------------------------------------------------------+
当我在没有按部分分组的情况下运行查询时可以看到:
SELECT answers.answer AS answers
FROM filledForm
JOIN prompts ON filledForm.formID = prompts.formID AND prompts.formID = 100
LEFT JOIN answers ON prompts.promptID = answers.promptID AND filledForm.filledFormID = answers.filledFormID
ORDER BY filledForm.filledFormID, prompts.promptID;
+-------------------+
| answers |
+-------------------+
| bob |
| NULL |
| NULL |
| 2023-03-16 |
| NULL |
| English |
| NULL |
| NULL |
| Johnny Depp |
| 60 |
| [email protected] |
| 2023-03-24 |
| NULL |
| German |
| NULL |
| NULL |
| Tom |
| 22 |
| [email protected] |
| 2023-03-29 |
| 11:00 |
| English |
| NULL |
| NULL |
+-------------------+
或者通过显示更多的列可能更容易理解:
SELECT filledForm.filledFormID, prompts.promptID, answers.answer AS answers
FROM filledForm
JOIN prompts ON filledForm.formID = prompts.formID AND prompts.formID = 100
LEFT JOIN answers ON prompts.promptID = answers.promptID AND filledForm.filledFormID = answers.filledFormID
ORDER BY filledForm.filledFormID, prompts.promptID;
+--------------+----------+-------------------+
| filledFormID | promptID | answer |
+--------------+----------+-------------------+
| 14 | 9 | bob |
| 14 | 10 | NULL |
| 14 | 11 | NULL |
| 14 | 12 | 2023-03-16 |
| 14 | 13 | NULL |
| 14 | 14 | English |
| 14 | 15 | NULL |
| 14 | 16 | NULL |
| 15 | 9 | Johnny Depp |
| 15 | 10 | 60 |
| 15 | 11 | [email protected] |
| 15 | 12 | 2023-03-24 |
| 15 | 13 | NULL |
| 15 | 14 | German |
| 15 | 15 | NULL |
| 15 | 16 | NULL |
| 16 | 9 | Tom |
| 16 | 10 | 22 |
| 16 | 11 | [email protected] |
| 16 | 12 | 2023-03-29 |
| 16 | 13 | 11:00 |
| 16 | 14 | English |
| 16 | 15 | NULL |
| 16 | 16 | NULL |
+--------------+----------+-------------------+
这些是完整的表格:
CREATE TABLE prompts (
promptID INT PRIMARY KEY AUTO_INCREMENT,
formID INT NOT NULL,
FOREIGN KEY (formID)
REFERENCES form (formID)
ON DELETE CASCADE,
hash VARCHAR(16) NOT NULL,
prompt TEXT(1023) NOT NULL,
changeDate TIMESTAMP
UNIQUE(formID, hash)
);
CREATE TABLE filledForm (
filledFormID INT PRIMARY KEY AUTO_INCREMENT,
formID INT NOT NULL,
FOREIGN KEY (formID)
REFERENCES form (formID)
ON DELETE CASCADE,
creationDate TIMESTAMP NOT NULL DEFAULT (UTC_TIMESTAMP),
INDEX (formID)
);
CREATE TABLE answers (
id INT PRIMARY KEY AUTO_INCREMENT,
filledFormID INT NOT NULL,
FOREIGN KEY (filledFormID)
REFERENCES filledForm (filledFormID)
ON DELETE CASCADE,
promptID INT NOT NULL,
FOREIGN KEY (promptID)
REFERENCES prompts (promptID)
ON DELETE CASCADE,
answer TEXT(99999),
INDEX (filledFormID)
);
SET SESSION group_concat_max_len = 1000000;
任何帮助将不胜感激。
您正在尝试
ORDER BY answers.promptID
,它位于您LEFT JOIN
的右侧。 ORDER BY prompts.promptID
代替。
SELECT
f.creationDate AS date,
GROUP_CONCAT(IFNULL(a.answer, "NULL") ORDER BY p.promptID) AS answers
FROM filledForm f
JOIN prompts p ON f.formID = p.formID AND p.formID = 100
LEFT JOIN answers a ON p.promptID = a.promptID AND f.filledFormID = a.filledFormID
GROUP BY f.filledFormID;