本文介紹了在子集中具有特定值的父級的 SQL 查詢的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我試圖只返回父級,其中子級集包含多個特定記錄.鑒于此表:
I am trying to return just the parent where the set of children contain multiple specific records. Given this table:
Product State
------- -----
111 AZ
111 CA
111 UT
222 AZ
222 WA
333 CA
我想找到同時具有 AZ
子記錄和 CA
子記錄的 Product
列表,即使它還有其他記錄.哪個會返回...
I want find the list of Product
that have both a AZ
child record and a CA
child record, even if it also has other records. Which would return ...
Product
-------
111
推薦答案
select product
from table
where state in ('az','ca')
group by product
having count(distinct(state)) = 2
select distinct product
from table
where state = 'az'
INTERSECT
select distinct product
from table
where state = 'ca'
select distinct t1.product
from table t1
join table t2
on t1.product = t2.product
and t1.state = 'az'
and t2.state = 'ca'
最后一個可能是最有效的.
The last is probably going to be the most efficient.
這篇關于在子集中具有特定值的父級的 SQL 查詢的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!