簡體   English   中英

SQL 具有相同第一列的查詢,在第二列中包含兩個值

[英]SQL Query with same first column, contains two values in the second column

這是一個例子,我不能使用原始數據,因為它可能是機密的

我正在嘗試獲取具有相同第一列的值,並在第二列中包含兩個項目。

例如,我想在同一次交付中顯示同時包含 MEAT 和 MILK 的所有交付(1000000001 和 1000000003)。

這是我所擁有的,我只是不知道使用什么條件來顯示具有相同第一列的項目,並包含所需的兩個項目。

SELECT DELIVERYNO, PRODUCT
FROM DELIVERY 
WHERE ........
DELIVERYNO  PRODUCT     
----------  ---------   
1000000001  MEAT
1000000001  EGGS
1000000001  MILK
1000000002  CHEESE
1000000002  MEAT
1000000003  MEAT
1000000003  CEREAL
1000000003  MILK

任何幫助/建議將不勝感激。

是這樣的嗎? 第 1 - 10 行中的示例數據; 您可能需要的查詢從第 11 行開始。

SQL> with test (deliveryno, product) as
  2    (select 1, 'meat'   from dual union all
  3     select 1, 'eggs'   from dual union all
  4     select 1, 'milk'   from dual union all
  5     select 2, 'cheese' from dual union all
  6     select 2, 'meat'   from dual union all
  7     select 3, 'meat'   from dual union all
  8     select 3, 'cereal' from dual union all
  9     select 3, 'milk'   from dual
 10    )
 11  select deliveryno
 12  from test
 13  where product in ('milk', 'meat')
 14  group by deliveryno
 15  having count(*) = 2;

DELIVERYNO
----------
         1
         3

SQL>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM