2015-03-31 67 views
0
name | value | product_id 
-------+--------+------------ 
color | red |   9 
style | modern |   9 
brand | xxx |   1 
color | blue |   4 
color | red |   1 

如何選擇產品ID,其中brand = xxx和color = red?這是id = 1的產品。選擇列作爲列

回答

0
SELECT product_id FROM tbl WHERE "name"='color' AND "value"='red' 
INTERSECT 
SELECT product_id FROM tbl WHERE "name"='brand' AND "value"='xxx'; 
0
SELECT * 
FROM YourTable T 
WHERE name = 'color' 
AND value = 'red' 
AND EXISTS(SELECT 1 FROM YourTable 
      WHERE name = 'brand' 
      AND value = 'xxx' 
      AND product_id = T.product_id) 
+0

哈!我不明白這個問題。 – 2015-03-31 20:25:18