gpt4 book ai didi

mysql - 处理空值最优雅的方法是什么

转载 作者:行者123 更新时间:2023-11-29 04:44:50 24 4
gpt4 key购买 nike

我有两个表和一个如下所示的查询::

select * from table_1
where x.column_1 in (
select column_2 from table_2
where column_3 = 'foo')

这几乎对我有用; column_1column_2 中有空值,我想将其视为匹配项。

我可以用这个写一个union:

-- query above
union all
select * from table_1
where column_2 is null
and exists (select * from table_2
where column_3 = 'foo'
and column_2 is null)

但是会扫描每个表两次,这看起来效率很低。

有没有一种方法可以将这两个查询结合起来,使查询更加高效?

最佳答案

试试这个

select * from table_1
where coalesce (column_1,'special value') in
(
select coalesce (column_2,'special value')
from table_2
where column_3 = 'foo'
)

当然,'特殊值' 不应包含在 table_2column_3 中,并且必须与列的数据类型兼容。

关于mysql - 处理空值最优雅的方法是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20880261/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com