需要查询A表的date时间字段的最小值,值有为NULL的,且不能使用MIN函数。
列如:
A表
| date | flag | work |
| 2020-01-01 | xxxxxx| xxxxxx |
| NULL | xxxxxx | xxxxxx |
| 2020-03-03 | xxxxxx| xxxxxx |
需要的结果:select 查询需要返回2020-01-01
select date from a where date is not null
order by date
limit 1;
或者
select date from a where nvl(date,'') != ''
order by date
limit 1;
select a.date from a where a.date!=null
order by date desc
limit 1
如果要查询如果要查询 A 表的 date 时间字段的最小值,但不能使用 MIN 函数,可以使用子查询和 WHERE 子句来实现。具体步骤如下:
SELECT MIN(date)
FROM A
WHERE date IS NOT NULL
这个子查询将返回 date 列中不为 NULL 的最小值。
SELECT sub_query.min_date
FROM (SELECT MIN(date) AS min_date
FROM A
WHERE date IS NOT NULL) AS sub_query
WHERE sub_query.min_date IS NOT NULL
这个 SELECT 语句将返回 date 列中不为 NULL 的最小值,如果最小值为 NULL,则返回空结果集。
在上面的 SELECT 语句中,首先执行子查询,返回 date 列中不为 NULL 的最小值,然后将其作为子查询表达式,放在一个 SELECT 语句中,使用 WHERE 子句筛选出最小值。如果最小值为 NULL,则 WHERE 子句将过滤掉这个结果,因此最终返回的结果中不包括 NULL。