首页 > 解决方案 > SQL 查询 - 只显示非空行

问题描述

如果所有列(开始日期、开始日期 2、开始日期 3)都有空值,我不想检索所有行

+--------+------------+-------------+-------------+
|   Id   | Start Date | Start Date2 | Start Date3 |
+--------+------------+-------------+-------------+
| ABC123 | 10/15/2021 | null        | null        |
| ABC123 | null       | null        | null        |
| ABC125 | 10/15/2021 | null        | null        |
| ABC126 | null       | null        | 10/15/2021  |
+--------+------------+-------------+-------------+

预期结果

+--------+------------+-------------+-------------+
|   Id   | Start Date | Start Date2 | Start Date3 |
+--------+------------+-------------+-------------+
| ABC123 | 10/15/2021 | null        | null        |
| ABC125 | 10/15/2021 | null        | null        |
| ABC126 | null       | null        | 10/15/2021  |
+--------+------------+-------------+-------------+

标签: sqlsql-servertsql

解决方案


您将使用一个where子句并检查每一列:

select t.*
from t
where start_date is not null or start_date2 is not null or start_date3 is not null;

在一个表中包含三个名为“开始日期”的列似乎很尴尬。


推荐阅读