Dav*_*vid 24 mysql group-concat
有没有选项让MySQL的Group_Concat函数包含空值?
请考虑源表中的以下示例:
userId, questionId, selectionId
7, 3, NULL
7, 4, 1
7, 5, 2
Run Code Online (Sandbox Code Playgroud)
当我使用GROUP_CONCAT查询选择表时,我得到以下内容:
7, 4=1,5=2
Run Code Online (Sandbox Code Playgroud)
我想得到以下内容:
7, 3=NULL,4=1,5=2
Run Code Online (Sandbox Code Playgroud)
作为参考,我的查询如下所示:
Select userId, GROUP_CONCAT(CONCAT(questionId, '=', selectionId))
From selection
Group by userId;
Run Code Online (Sandbox Code Playgroud)
我也试过像这样添加一个IFNULL:
Select userId, GROUP_CONCAT(IFNULL(CONCAT(questionId, '=', selectionId), 'NULL'))
From selection
Group by userId;
Run Code Online (Sandbox Code Playgroud)
但这产生了以下结果:
7, NULL,4=1,5=2
Run Code Online (Sandbox Code Playgroud)
注意 - 还有一个我忘了包含的复杂性.selectionId是另一个表的外键.我使用左外连接到selection_text表.我的真实查询包括该表中的字段(这些字段解析为NULL,因为selectionId为null).
Joa*_*son 37
你应该只是可以IFNULL的列NULL;
SELECT userId, GROUP_CONCAT(CONCAT(questionId, '=',
IFNULL(selectionId, 'NULL')))
FROM selection
GROUP BY userId;
Run Code Online (Sandbox Code Playgroud)
在这里演示.
您应该直接在值上使用IFNULL或:COALESCEselectionId
SELECT
userId,
GROUP_CONCAT(CONCAT(questionId, '=', COALESCE(selectionId, 'NULL')))
FROM selection
GROUP BY userId;
Run Code Online (Sandbox Code Playgroud)