我有3个表:产品,订单和 orderLines (order_id,product_id)。
我有一个SQL查询来确定在only one query
中几乎不可能做到的事情
有没有办法只有一个查询:
order A
:显示product1
,product2
..首先出现在orderA's orderLines
,然后显示following products
(未排序)。 PS:
我知道可以通过union
两个查询实现此目标,但最好在only one query
中完成此操作。
答案 0 :(得分:4)
您可以在order by
子句中放置子查询。在这种情况下,您需要exists
子查询:
select p.*
from products p
order by (exists (select 1
from orderlines ol
where p.productid = ol.productid and o.orderid = ORDERA
)
) desc;