0

我正在尝试在订单行上搜索包含一篇特定文章的订单,但如果有不止一行,我不想看到任何行。

这是两个订单的示例,我只想获得一个结果。订单 9891026 有两排,而 9891025 只有一排。

    select  order_no, line_no
    from customer_order_line
    where order_no in('9891026','9891025')

结果

订单号 line_no
9891026 1
9891026 2
9891025 1

我只想得到

订单号 line_no
9891025 1

我不知道如何对此进行正确的查询。

4

3 回答 3

1

一种方法是检查existsline_no 是否大于 1 的订单:

select ol.order_no, ol.line_no
from customer_order_line ol
where ol.order_no in (9891026, 9891025)
and not exists (
  select * from customer_order_line x
  where x.order_no = ol.order_no and x.line_no > 1
);
于 2021-10-28T10:20:27.510 回答
0

或者,您可以执行 aGROUP BY并使用HAVING以确保那里只有一行。

select ol.order_no, min(ol.line_no)
from customer_order_line ol
where ol.order_no in ('9891026', '9891025')
group by ol.order_no
having count(*) = 1
于 2021-10-28T10:47:41.680 回答
0

首先找到单行订单号order_no(子查询),然后仅从具有这些订单号的订单中进行选择。请注意,连接查询(虽然可能更难阅读)可能更有效。

select * 
from customer_order_line 
where order_no in
(
 select order_no 
 from customer_order_line
 group by order_no 
 having count(*) = 1
) -- this is the list of single-item orders
and order_no in (9891026,9891025) -- this is the list of target orders
于 2021-10-28T10:57:18.613 回答