2017-03-22 108 views
1

我有兩個數據庫表,它們都有大量的行。第一張表格包含產品概述,第二張表格提供產品的附加信息。該表看起來如下:Mysql查詢 - 如何包含「過濾器」

 

    #Products 
    + ---------- + ------ + ------------ + 
    | Product_Id | Status | EAN   | 
    + ---------- + ------ + ------------ + 
    | 1   | 1  || 
    | 2   | 1  || 
    | 3   | 1  | 6548214656 | 
    | 4   | 2  | 245511411241 | 
    | 5   | 1  | 8888888888 | 
    | etc.  | etc . | etc.   | 
    + ---------- + ------ + ------------ + 

 


    #Info 
     + ------- + ------------ + --------- + ---------- + 
     | Info_Id | EAN   | Info_Type | Info_Value | 
     + ------- + ------------ + --------- + ---------- + 
     | 1  || brand  | brand1  | 
     | 2  || type  | type1  | 
     | 3  || price  | 0.00  | 
     | 4  || brand  | brand6  | 
     | 5  || type  | type3  | 
     | 6  || price  | 15.00  | 
     | 7  | 6548214656 | brand  | brand34 | 
     | 8  | 6548214656 | type  | type1  | 
     | 9  | 6548214656 | price  | 99.00  | 
     | 10  | 245511411241 | brand  | brand324 | 
     | 11  | 245511411241 | type  | type1  | 
     | 12  | 245511411241 | price  | 98.00  | 
     | 13  | 8888888888 | brand  | brand1  | 
     | 14  | 8888888888 | price  | 9.00  | 
     | 14  | 8888888888 | type  | type4  | 
     | etc. | etc.   | etc.  | etc.  | 
     + ------- + ------------ + --------- + ---------- + 

用下面的查詢我能夠找到匹配對應產品addtional信息EAN的:

 


    select i.EAN 
      , p.ProductStatus 
      , max(case info_type when 'brand' then info_value end) as brand 
      , max(case info_type when 'type' then info_value end) as [type] 
      , max(case info_type when 'price' then info_value end) as price 
     from #Info i 
     inner join #Products p on p.ean = i.ean 
     WHERE p.Status=1 
     group by i.EAN, p.ProductStatus ORDER BY P.Product_id DESC LIMIT 10 

這爲我提供瞭如下表:

 


    + ------------ + ------ + -------- + ----- + ----- + 
     | EAN   | Status | brand | type | price |  
     + ------------ + ------ + -------- + ----- + ----- + 
     || 1  | brand1 | type1 | 0.00 | 
     | 6548214656 | 1  | brand34 | type1 | 99.00 | 
     || 1  | brand6 | type3 | 15.00 | 
     | 8888888888 | 1  | brand1 | type4 | 9.00 | 
     | etc. (10 products)        | 
     + ------------ + ------ + -------- + ----- + ----- + 

但是,現在我想能夠篩選特定品牌,類型或價格。例如,我只想選擇價格大於50的產品(Info_Type = price AND Info_Value> 50.00),其次品牌是特定品牌,可以說「brand1」(Info_Type = brand AND Info_Value = brand1)。有人能告訴我如何將這個包含到我的查詢中嗎?試了很多東西,最貼近我來到了期望的結果是下面的查詢:

 

    select i.EAN 
     , p.ProductStatus 
     , max(case when (info_type = 'brand' AND info_value='brand1') then info_value end) as brand 
     , max(case info_type when 'type' then info_value end) as [type] 
     , max(case info_type when 'price' then info_value end) as price 
    from #Info i 
    inner join #Products p on p.ean = i.ean 
    WHERE p.Status=1 
    group by i.EAN, p.ProductStatus ORDER BY P.Product_id DESC LIMIT 10 

然而,這提供了以下結果:

 
    + ------------ + ------ + -------- + ----- + ----- + 
    | EAN   | Status | brand | type | price |  
    + ------------ + ------ + -------- + ----- + ----- + 
    || 1  | brand1 | type1 | 0.00 | 
    | 6548214656 | 1  | NULL  | type1 | 99.00 | 
    || 1  | NULL  | type3 | 15.00 | 
    | 8888888888 | 1  | brand1 | type4 | 9.00 | 
    | etc. (10 products)        | 
    + ------------ + ------ + -------- + ----- + ----- + 

我想看到以下結果:

 
    + ------------ + ------ + -------- + ----- + ----- + 
    | EAN   | Status | brand | type | price |  
    + ------------ + ------ + -------- + ----- + ----- + 
    || 1  | brand1 | type1 | 0.00 | 
    | 8888888888 | 1  | brand1 | type4 | 9.00 | 
    | etc. (10 products)        | 
    + ------------ + ------ + -------- + ----- + ----- + 

誰幫我出去? :)

回答

0

您可以使用having

select i.EAN, p.ProductStatus, 
     max(case when (info_type = 'brand' AND info_value='brand1') then info_value end) as brand, 
     max(case info_type when 'type' then info_value end) as [type], 
     max(case info_type when 'price' then info_value end) + 0 as price 
from #Info i inner join 
    #Products p 
    on p.ean = i.ean 
where p.Status=1 
group by i.EAN, p.ProductStatus 
having price < 10 
order by P.Product_id DESC 
LIMIT 10; 

+ 0是因爲info_value似乎被存儲爲一個字符串。然而,你想Price是一個數字,所以這將它轉換爲一個數字。

+0

但後來我錯過了品牌選擇,對吧? – mh3982

+0

@ mh3982。 。 。該查詢基於您問題中的查詢。我正在指出如何使用'having'進行過濾。 –

+0

Thnx。價格選擇的+0在哪裏?無論如何,這似乎是解決方案!非常感謝你! – mh3982