GroupConcat 中缺少空值
Missing null values in a GroupConcat
我有一个 table,我在其中注册活动开始和结束的时间。我想要一个在我的日期范围内结束或正在进行的事件的列表,但那些尚未结束的事件是错误的,因为组 concat 中缺少空值。
如何通过 entity_id 分组获得空值?
| id | entity_id | start | end |
|----|-----------|----------------------|----------------------|
| 1 | 10 | 2020-05-22T23:50:00Z | 2020-05-23T00:15:00Z |
| 2 | 20 | 2020-05-22T23:30:00Z | 2020-05-23T00:50:00Z |
| 3 | 10 | 2020-05-23T01:00:00Z | 2020-05-23T01:10:00Z |
| 4 | 20 | 2020-05-23T01:30:00Z | 2020-05-23T01:50:00Z |
| 5 | 20 | 2020-05-23T02:00:00Z | null |
| 6 | 10 | 2020-05-23T05:00:00Z | null |
我的查询:
SELECT entity_id,
SUBSTRING_INDEX(GROUP_CONCAT(`start` ORDER BY `start` DESC SEPARATOR '||'), '||', 1) AS `start`,
SUBSTRING_INDEX(GROUP_CONCAT(`end` ORDER BY `start` DESC SEPARATOR '||'), '||', 1) AS `end`
FROM my_table
WHERE `start` <= '2020-05-23 23:59:59'
AND ( `end` IS NULL OR `end` BETWEEN '2020-05-23 00:00:00' AND '2020-05-23 23:59:59' )
GROUP BY entity_id ;
这是查询结果
| entity_id | start | end |
|-----------|---------------------|---------------------|
| 10 | 2020-05-23 05:00:00 | 2020-05-23 01:10:00 |
| 20 | 2020-05-23 02:00:00 | 2020-05-23 01:50:00 |
但这是应该的
| entity_id | start | end |
|-----------|---------------------|---------------------|
| 10 | 2020-05-23 05:00:00 | null |
| 20 | 2020-05-23 02:00:00 | null |
我创建了一个示例:http://sqlfiddle.com/#!9/e04be2/1/0
GROUP_CONCAT()
跳过 NULL
个值。
您可以使用 IFNULL
将空值转换为显式字符串 'null'
。
SELECT entity_id,
SUBSTRING_INDEX(GROUP_CONCAT(`start` ORDER BY `start` DESC SEPARATOR '||'), '||', 1) AS `start`,
SUBSTRING_INDEX(GROUP_CONCAT(IFNULL(`end`, 'null') ORDER BY `start` DESC SEPARATOR '||'), '||', 1) AS `end`
FROM my_table
WHERE `start` <= '2020-05-23 23:59:59'
AND ( `end` IS NULL OR `end` BETWEEN '2020-05-23 00:00:00' AND '2020-05-23 23:59:59' )
GROUP BY entity_id ;
我有一个 table,我在其中注册活动开始和结束的时间。我想要一个在我的日期范围内结束或正在进行的事件的列表,但那些尚未结束的事件是错误的,因为组 concat 中缺少空值。
如何通过 entity_id 分组获得空值?
| id | entity_id | start | end |
|----|-----------|----------------------|----------------------|
| 1 | 10 | 2020-05-22T23:50:00Z | 2020-05-23T00:15:00Z |
| 2 | 20 | 2020-05-22T23:30:00Z | 2020-05-23T00:50:00Z |
| 3 | 10 | 2020-05-23T01:00:00Z | 2020-05-23T01:10:00Z |
| 4 | 20 | 2020-05-23T01:30:00Z | 2020-05-23T01:50:00Z |
| 5 | 20 | 2020-05-23T02:00:00Z | null |
| 6 | 10 | 2020-05-23T05:00:00Z | null |
我的查询:
SELECT entity_id,
SUBSTRING_INDEX(GROUP_CONCAT(`start` ORDER BY `start` DESC SEPARATOR '||'), '||', 1) AS `start`,
SUBSTRING_INDEX(GROUP_CONCAT(`end` ORDER BY `start` DESC SEPARATOR '||'), '||', 1) AS `end`
FROM my_table
WHERE `start` <= '2020-05-23 23:59:59'
AND ( `end` IS NULL OR `end` BETWEEN '2020-05-23 00:00:00' AND '2020-05-23 23:59:59' )
GROUP BY entity_id ;
这是查询结果
| entity_id | start | end |
|-----------|---------------------|---------------------|
| 10 | 2020-05-23 05:00:00 | 2020-05-23 01:10:00 |
| 20 | 2020-05-23 02:00:00 | 2020-05-23 01:50:00 |
但这是应该的
| entity_id | start | end |
|-----------|---------------------|---------------------|
| 10 | 2020-05-23 05:00:00 | null |
| 20 | 2020-05-23 02:00:00 | null |
我创建了一个示例:http://sqlfiddle.com/#!9/e04be2/1/0
GROUP_CONCAT()
跳过 NULL
个值。
您可以使用 IFNULL
将空值转换为显式字符串 'null'
。
SELECT entity_id,
SUBSTRING_INDEX(GROUP_CONCAT(`start` ORDER BY `start` DESC SEPARATOR '||'), '||', 1) AS `start`,
SUBSTRING_INDEX(GROUP_CONCAT(IFNULL(`end`, 'null') ORDER BY `start` DESC SEPARATOR '||'), '||', 1) AS `end`
FROM my_table
WHERE `start` <= '2020-05-23 23:59:59'
AND ( `end` IS NULL OR `end` BETWEEN '2020-05-23 00:00:00' AND '2020-05-23 23:59:59' )
GROUP BY entity_id ;