【发布时间】:2019-07-04 12:44:28
【问题描述】:
目前,我正在使用将在相关表中进行搜索的参数进行复杂查询,这非常有效,但如果我需要说“我想找到没有特定条件的人”之类的话,我需要两次编写相同的 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 子句。
【问题讨论】:
标签: sql postgresql postgresql-9.6