我正在编写SQL过滤器并且
User_Condition
中的空间有限,所以我想知道在我运行的嵌套查询之前是否可以使用OR。
这是一个有效的基本代码示例,以及我认为我想要完成的一个示例。
select *
from tlorder
where detail_line_id = '3806526'
and (origpc in (select zone_id
from zone
where (zone_id = origpc
and service_type = 'DIRECT' or zone_id = destpc
and service_type <> 'DIRECT'))
or desctpc in (select zone_id
from zone
where (zone_id = origpc and service_type = 'DIRECT' or
zone_id = destpc and service_type <> 'DIRECT')))
从和子句开始是过滤器值,但我想知道是否有类似这样的选项:
select *
from tlorder
where detail_line_id = '3806526'
and (origpc OR destpc) in (select zone_id
from zone
where (zone_id = origpc and service_type = 'DIRECT' or
zone_id = destpc and service_type <> 'DIRECT'))
- 注意我无法编辑突出显示的部分,因此构成了最大的问题。
答案 0 :(得分:1)
(
origpc IN (SELECT zone_id FROM zone
WHERE ( (
zone_id = origpc
AND service_type = 'DIRECT'
)
OR
(
zone_id = destpc
AND service_type <> 'DIRECT'
)
)
OR destpc IN (SELECT zone_id FROM zone
WHERE ( (
zone_id = origpc
AND service_type = 'DIRECT'
)
OR
(
zone_id = destpc
AND service_type <> 'DIRECT'
)
)
)
根据您的屏幕截图,这应该有效。
答案 1 :(得分:1)
最好的办法是将限制逻辑添加到子查询的where子句中,并将IN更改为EXISTS。实际上,由于您过滤子查询中的行的方式,您已经几乎完成了此操作。这应该适合你:
select *
from tlorder
where detail_line_id = '3806526'
and EXISTS (select 1
from zone
where (zone_id = origpc and service_type = 'DIRECT') or
(zone_id = destpc and service_type <> 'DIRECT')
)
请注意,您可以选择存在中的任何值,我只使用1,因为它很简单并且不会减慢任何速度。另外,我在子查询中的括号内更具体。
要回答您的具体问题,不能在IN语句的左侧使用多个列。但是,当您没有使用子查询时,可以在IN的右侧。例如:
select *
from tlorder
where detail_line_id = '3806526'
and 123 in (origpc, destpc)