我希望通过使用where-statement
将这种令人困惑的左外连接语句简化为易于阅读的内容Select approved.orderId
From (select distinct orderId from OrderLine where isApproved = 1 and isDismissed = 0) approved
left outer join
(select distinct orderId from OrderLine where isApproved = 0 and isDismissed = 0) unapproved
on approved.orderId = unapproved.orderId
Where unapproved.orderId Is Null
类似于:
Select orderId
From (select distinct orderId from OrderLine where (isApproved = 1 and isDismissed = 0) OR (isApproved = 0 and isDismissed = 0 and orderId is null))
答案 0 :(得分:1)
这应该做:
SELECT DISTINCT O.orderId
FROM OrderLine O
WHERE O.isDismissed = 0
AND O.isApproved = 1
AND NOT EXISTS( SELECT 1 FROM OrderLine
WHERE isApproved = 0
AND isDismissed = 0
AND orderId = O.OrderId)
答案 1 :(得分:0)
看起来联接正在尝试获得至少有一条已批准的行但没有未批准的行的订单。这应该等同于
select distinct orderId
from OrderLine
where isApproved = 1
and isDismissed = 0
AND orderID NOT IN
(select orderId
from OrderLine
where isApproved = 0
and isDismissed = 0)
答案 2 :(得分:0)
except
应该可以很快地用于您的用例,并且不再需要连接和内部查询:
SELECT DISTINCT orderId
FROM orderline
WHERE isApproved = 1 and isDismissed = 0
EXCEPT
SELECT orderId
FROM orderline
WHERE isApproved = 0 and isDismissed = 0