![](/img/trans.png)
[英]PostgreSQL: SELECT DISTINCT vs SELECT DISTINCT ON (id)
[英]Select distinct id with two types postgresql
我有兩種類型的訂單編號,我需要選擇僅存在一種的不同訂單編號。
數據如下:
orderid 1 type 1
orderid 1 type 2
orderid 2 type 1
orderid 2 type 1
我想選擇僅類型1存在的訂單ID。
我試過了:
select distinct orderid from orders where type=1 and type<>2
這將同時返回訂單編號1和2
您可以使用except
來執行此操作。
select orderid from orders where type = 1
except
select orderid from orders where type <> 1
您可以使用group by
:
select orderid
from orders
group by orderid
having min(type) = 1 and max(type) = 1;
無需distinct
。 一個orderid
僅返回一次。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.