如何使用 COALESCE 在 WHERE IN 中模拟 "IF/ELSE" 多个值?
How can I use COALESCE to simulate "IF/ELSE" inside WHERE IN for multiple values?
如何使用 COALESCE 或其他工具实现此目的?
,但这种方法不同,因为我没有加入 table。
大小写不起作用,因为它不能 return 多个值。
select *
from data
where fieldId in (
--- IF there are IDs here, use this ---
select fieldId
from relationNameFromField
where relationSettingId in (
select rs.id
from relationSetting rs
left join entity e on e.id = rs.entityId
where e.uid = 'R0izCPXH46'
)
--- ELSE use this when the above returns nothing ---
select id from field
)
如果你会用cte那么
with
cte as(select fieldId
from relationNameFromField
where relationSettingId in (
select rs.id
from relationSetting rs
left join entity e on e.id = rs.entityId
where e.uid = 'R0izCPXH46'
),
cte1 as
(select id from field where (select count(*) from cte) = 0),
cte2 as
(select fieldid from cte
union all
select id from cte1)
select *
from data
join cte2 on fieldId = cte2.id;
如何使用 COALESCE 或其他工具实现此目的?
大小写不起作用,因为它不能 return 多个值。
select *
from data
where fieldId in (
--- IF there are IDs here, use this ---
select fieldId
from relationNameFromField
where relationSettingId in (
select rs.id
from relationSetting rs
left join entity e on e.id = rs.entityId
where e.uid = 'R0izCPXH46'
)
--- ELSE use this when the above returns nothing ---
select id from field
)
如果你会用cte那么
with
cte as(select fieldId
from relationNameFromField
where relationSettingId in (
select rs.id
from relationSetting rs
left join entity e on e.id = rs.entityId
where e.uid = 'R0izCPXH46'
),
cte1 as
(select id from field where (select count(*) from cte) = 0),
cte2 as
(select fieldid from cte
union all
select id from cte1)
select *
from data
join cte2 on fieldId = cte2.id;