gpt4 book ai didi

sql - 用函数反转 where 子句,这可能吗?

转载 作者:行者123 更新时间:2023-11-29 14:08:19 26 4
gpt4 key购买 nike

目前,我正在使用将在相关表中进行搜索的参数进行复杂查询,这非常有效,但如果我需要说“我想找到不符合特定条件的人”之类的话,我需要两次编写相同的 where 子句:一次使用 IN,一次使用 NOT IN。有没有办法避免这种情况?类似于 functionX(select id from tablex): boolean

目前我有这样的东西:

   select * from tpatient 
where
(includeparameter1 and TPatient.Id in
(select patientid from tdoctorvisit where x ilike parameter1)
)
or (
(includeparameter1 = false) and TPatient.Id not in (
select patientid from tdoctorvisit where x ilike parameter1)
)

这可以以某种方式改进下面的查询吗?

    select * from tpatient where 
functionX(includeparameter1, TPatient.id,
select patientid from tdoctorvisit where x ilike parameter1)

这将使我的查询更小一些,因为我有许多 where 子句。

最佳答案

我想你可以这样写:

WHERE includeparameter1 = TPatient.Id in (select patientid from tdoctorvisit where x ilike parameter1)

因为:

  • includeparameter1 = a
  • TPatient.Id in (...) = b

所以你有条件:

WHERE (a = true AND b = true) OR (a = false AND b = false)

这与 WHERE a = b 相同

关于sql - 用函数反转 where 子句,这可能吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56888240/

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