簡體   English   中英

根據同一列上的where條件使用不同的過濾選項從表中選擇詳細信息

[英]selecting details from the table based on the where condition on same column with different filtering option

我有一個具有以下指定結構的表

表結構

從表中,我只想檢索產品ID,該產品ID的Ram值為12 ,顏色為Blue 預期結果為1。我嘗試了許多查詢,但未共享預期結果。

解決辦法是什么?

由於我們具有一組未定義的功能,因此很難為每個功能管理單獨的表。

您可以使用條件聚合:

select productid
from t
group by productid
having max(case when feature = 'Ram' then value end) = '12' and
       max(case when feature = 'Color' then value end) = 'Blue';

使用不存在的相關子查詢

select distinct product_id from tablename a
where not exists 
     (select 1 from tablename b where a.product_id=b.product_id and feature='Ram' and value<>12)
and not exists 
     (select 1 from tablename c where a.product_id=c.product_id and feature='Color' and value<>'blue')

暫無
暫無

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

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