gpt4 book ai didi

sql - 为什么在sql语句中使用COALESCE()?

转载 作者:可可西里 更新时间:2023-11-01 07:33:44 26 4
gpt4 key购买 nike

我正在重构一些旧代码并偶然发现了这个命名查询(它在 mysql 之上使用 hibernate):

delete F from
foo F
inner join user DU on F.user_id = DU.id
where
(COALESCE(:userAlternateId,null) is null or DU.alternate_id like :userAlternateId )
and ( COALESCE(:fooId,null) is null or F.foo_id like :fooId )
and (
( COALESCE(:fromUpdated,null) is null or F.updated_at>=:fromUpdated )
and ( COALESCE(:toUpdated,null) is null or F.updated_at<=:toUpdated )
)

我不明白为什么要以这种方式使用此 COALESCE:COALESCE(:userAlternateId,null) 为空

这是一个性能黑客还是它使查询数据库独立或......?

顺便说一句,userAlternateId 是一个字符串/varchar,其他 id 是长整数,from-to 是日期

最佳答案

我想不出以这种方式使用 COALESCE 的任何理由。

下面的语句是等价的

DELETE  F 
FROM foo F
INNER JOIN User DU on F.user_id = DU.id
WHERE (:userAlternateId IS NULL OR DU.alternate_id LIKE :userAlternateId)
AND (:fooId IS NULL OR F.foo_id LIKE :fooId)
AND (:fromUpdated IS NULL OR F.updated_at >= :fromUpdated)
AND (:toUpdated IS NULL OR F.updated_at <= :toUpdated)

关于sql - 为什么在sql语句中使用COALESCE()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3830045/

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