给定以下MySQL查询:
SELECT
`show`.`id`
, GROUP_CONCAT( `showClips`.`clipId` ORDER BY `position` ASC ) AS 'playlist'
FROM
`show`
INNER JOIN
`showClips`
ON
( `show`.`id` = `showClips`.`showId` )
;
我想从数据库中检索所有“显示”的列表,包括所包含的“剪辑”的ids。
只要show
表中有条目,就可以很好地工作。对于这个问题,我们假设所有的表都是完全空的。
GROUP_CONCAT
将返回NULL
,从而强制在结果中添加一行(只包含NULL
值)。
然后,我的应用程序将认为存在一个show/result。但是这个结果将是无效的。这当然可以检查,但我觉得这可以(也应该)在查询中被阻止。
发布于 2010-09-06 23:30:46
您只需在末尾添加一个GROUP BY
即可。
测试用例:
CREATE TABLE `show` (id int);
CREATE TABLE `showClips` (clipId int, showId int, position int);
SELECT
`show`.`id`,
GROUP_CONCAT( `showClips`.`clipId` ORDER BY `position` ASC ) AS 'playlist'
FROM `show`
INNER JOIN `showClips` ON ( `show`.`id` = `showClips`.`showId` )
GROUP BY `show`.`id`;
Empty set (0.00 sec)
发布于 2010-09-06 23:30:10
添加group by show
.id
,则空表结果正确:
create table emptyt(id int,name varchar(20));
select id, group_concat(name) from emptyt
结果:
NULL, NULL
使用group by查询
select id, group_concat(name) from emptyt
group by Id
结果:
空数据集
https://stackoverflow.com/questions/3652580
复制相似问题