2016-01-18 84 views
-1

我有SQL表生成的3列由ERP。查詢SQL查詢的常見屬性

+-----------+-----------+------------+ 
| ProductID | Attribute | Value | 
+-----------+-----------+------------+ 
|  100 | Size  | Big  | 
|  100 | Color  | Red  | 
|  100 | Weight | Heavy  | 
|  200 | Size  | Small  | 
|  200 | Color  | Red  | 
|  200 | Weight | Light  | 
|  300 | Size  | Big  | 
|  300 | Color  | Green  | 
|  300 | Weight | Heavy  | 
+-----------+-----------+------------+ 

我想要查詢表以查找具有匹配屬性的產品。例如

SELECT * FROM Table 
WHERE Attribute ='Size' AND Value = 'Big' AND Attribute ='Weight' AND Value = 'Heavy' 

所以退回產品100和300

+2

您還沒有解釋「找到具有匹配屬性的產品」的含義:什麼是輸入?就這張表,還是其他的一些,還是一些參數/常量?爲什麼在您要求查詢時提供查詢?期望的查詢應該是什麼樣的?根據輸入,輸出應該是什麼樣子?請舉例輸入和輸出。 PS爲什麼兩個不同的DBMS標籤? – philipxy

+0

MySQL和/或MS SQL Server? (不要標記不涉及的產品...) – jarlh

回答

1

另一種方法是使用傳統的「數據透視查詢」並將其視爲「派生表」,然後將大小/重量/顏色作爲行進行篩選。

SQL Fiddle

的MySQL 5.6架構設置

CREATE TABLE ERPout 
    (`ProductID` int, `Attribute` varchar(6), `Value` varchar(5)) 
; 

INSERT INTO ERPout 
    (`ProductID`, `Attribute`, `Value`) 
VALUES 
    (100, 'Size', 'Big'), 
    (100, 'Color', 'Red'), 
    (100, 'Weight', 'Heavy'), 
    (200, 'Size', 'Small'), 
    (200, 'Color', 'Red'), 
    (200, 'Weight', 'Light'), 
    (300, 'Size', 'Big'), 
    (300, 'Color', 'Green'), 
    (300, 'Weight', 'Heavy') 
; 

查詢1

SELECT 
     ProductID 
    , Size 
    , Color 
    , Weight 
FROM (
     SELECT 
      ProductID 
      , MAX(CASE WHEN Attribute = 'Size' THEN VALUE END) AS Size 
      , MAX(CASE WHEN Attribute = 'Color' THEN VALUE END) AS Color 
      , MAX(CASE WHEN Attribute = 'Weight' THEN VALUE END) AS Weight 
     FROM ERPout 
     GROUP BY 
      ProductID 
    ) p 
WHERE Size = 'Big' 
     AND Weight = 'Heavy' 

Results

| ProductID | Size | Color | Weight | 
|-----------|------|-------|--------| 
|  100 | Big | Red | Heavy | 
|  300 | Big | Green | Heavy | 
+0

老兄,它的工作更好。非常感謝好友。你是一個拯救生命的人。 –

0

有幾種不同的方式來做到這一點。這裏使用條件聚集一個是:

select productid 
from yourtable 
group by productid 
having max(case when attribute = 'Size' then value end) = 'Big' 
    and max(case when attribute = 'Weight' then value end) = 'Heavy' 
1

可以使用做一個條件SUM

SELECT ProductID 
FROM tbl 
GROUP BY ProductID 
HAVING 
    SUM(
     CASE 
      WHEN Attribute = 'Size' AND Value = 'BIG' THEN 1 
      WHEN Attribute = 'Weight' AND Value = 'Heavy' THEN 1 
      ELSE 0 
     END 
    ) = 2 

您可以使用>= 2如果你想ProductID s的其他屬性。

0

我相信你想要的產品對。此外,我並沒有對任何地方的值進行硬編碼。如果您需要更改屬性集,請修改having子句。

select t1.ProductID, t2.ProductID 
from 
    T t1 inner join T t2 
     on  t2.ProductID > t1.ProductID 
      and t2.Attribute = t1.Attribute and t2.Value = t1.Value 
group by 
    t1.ProductID, t2.ProductID 
having 
     count(case when t1.Attribute = 'Size' then 1 end) = 1 
    and count(case when t1.Attribute = 'Weight' then 1 end) = 1 

我有點想回答像費利克斯的答案是你想要的。

+0

謝謝@ shawnt00 –

0

如果你只需要的ProductID,這將是一個高效短sollution:

SELECT ProductID 
FROM product_info 
WHERE Attribute ='Size' AND Value = 'Big' 
    OR Attribute ='Weight' AND Value = 'Heavy' 
GROUP BY ProductID 
HAVING COUNT(ProductID) = 2 

如果你需要在一排所有屬性和已定義的(唯一/主)上(的ProductID鍵,屬性),你可能想使用這樣的查詢:

SELECT size.ProductID, size.Value Size, color.Value Color, weight.Value Weight 
FROM product_info size 
JOIN product_info weight USING(ProductID) 
LEFT JOIN product_info color 
    ON color.ProductID = size.ProductID 
    AND color.Attribute = 'Color' 
WHERE size.Attribute = 'Size' AND size.Value = 'Big' 
    AND weight.Attribute = 'Weight' AND weight.Value = 'Heavy' 

如果您另外定義上的索引,性能會更好。

這兩個查詢都使用WHERE子句避免完整的表/索引掃描。根據表格大小和數據,這可能是一個巨大的優勢,因爲早期的過濾。