代码之家  ›  专栏  ›  技术社区  ›  Oliver Salzburg

当没有输入数据时,如何防止组CONCAT创建结果?

  •  19
  • Oliver Salzburg  · 技术社区  · 14 年前

    给定以下MySQL查询:

    SELECT
      `show`.`id`
      , GROUP_CONCAT( `showClips`.`clipId` ORDER BY `position` ASC ) AS 'playlist'
    FROM
      `show`
      INNER JOIN
        `showClips`
          ON
            ( `show`.`id` = `showClips`.`showId` )
    ;
    

    我想从数据库中检索所有“显示”的列表,包括包含的“剪辑”的ID。

    只要在 show 桌子。对于这个问题,假设所有表都是空的。

    GROUP_CONCAT NULL 从而将一行强制放入结果中(其中只包含 无效的

    2 回复  |  直到 14 年前
        1
  •  43
  •   Daniel Vassallo    14 年前

    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)
    
        2
  •  10
  •   Michael Pakhantsov    14 年前

    添加分组依据 show . id

    创建表emptyt(id int,name varchar(20));

       select id, group_concat(name) from emptyt
    

     NULL, NULL
    

     select id, group_concat(name) from emptyt
     group by Id
    

    结果:

    空数据集