2017-02-17 47 views
1

我有3個表格。第一個有我想要的記錄。另外兩個類別可以應用於第一個表格。如果在描述中找到table3的查找值,我想返回該類別。否則,返回table2中的類別。我認爲我的邏輯是正確的,但結果正在倍增。我怎樣才能將結果限制爲我想要的table1記錄,但應用正確的類別?PostgreSQL 3表連接乘法

這是我的查詢與示例架構。它應該只在表1與哪個類別是正確的返回第6行,但它返回10 http://sqlfiddle.com/#!15/fc6fa/49/0

SELECT table1.product_code, table1.date_signed, table1.description, 
CASE 
    WHEN lower(table1.description) LIKE ('%' || lower(table3.lookup_value) || '%') 
    THEN table3.category 
    ELSE table2.category 
END 
FROM table1 
LEFT JOIN table2 ON table2.psc_code = table1.product_code 
LEFT JOIN table3 ON table3.psc_code = table1.product_code 
WHERE date_signed = '2017-02-01'; 



create table table1 ( 
product_code int, 
date_signed timestamp, 
description varchar(20) 
); 

insert into table1 
(product_code, date_signed, description) 
values 
(1, '2017-02-01', 'i have a RED car'), 
(2, '2017-02-01', 'i have a blue boat'), 
(3, '2017-02-01', 'i have a dark cat'), 
(1, '2017-02-01', 'i have a green truck'), 
(2, '2017-02-01', 'i have a blue rug'), 
(3, '2017-02-01', 'i have a dark dog'), 
(1, '2017-02-02', 'i REd NO SHOW'), 
(2, '2017-02-02', 'i blue NO SHOW'), 
(3, '2017-02-02', 'i dark NO SHOW'); 

create table table2 ( 
psc_code int, 
category varchar(20) 
); 

insert into table2 
(psc_code, category) 
values 
(1, 'vehicle'), 
(2, 'vehicle'); 

create table table3 ( 
psc_code int, 
lookup_value varchar(20), 
category varchar(20) 
); 

insert into table3 
(psc_code, lookup_value, category) 
values 
(1, 'fox', 'animal'), 
(1, 'red', 'color'), 
(1, 'box', 'shipping'), 
(2, 'cat', 'animal'); 

回答

2

您試圖加入1到很多,而您只需要一個值。

SELECT table1.product_code, table1.date_signed, table1.description, 
CASE 
    WHEN EXISTS (select 1 from table3 
       where table3.psc_code = table1.product_code and 
        lower(table1.description) LIKE ('%' || lower(table3.lookup_value) || '%')) 
    THEN (select table3.category from table3 
     where table3.psc_code = table1.product_code and 
       lower(table1.description) LIKE ('%' || lower(table3.lookup_value) || '%') limit 1) 
    ELSE (select table2.category 
     from table2 
     where table2.psc_code = table1.product_code 
     limit 1) 
END 
FROM table1 
WHERE date_signed = '2017-02-01'; 

http://rextester.com/TQIY93378

+--------------+---------------------+----------------------+----------+ 
| product_code |  date_signed  |  description  | category | 
+--------------+---------------------+----------------------+----------+ 
| 1   | 01.02.2017 00:00:00 | i have a RED car  | color | 
| 2   | 01.02.2017 00:00:00 | i have a blue boat | vehicle | 
| 3   | 01.02.2017 00:00:00 | i have a dark cat | NULL  | 
| 1   | 01.02.2017 00:00:00 | i have a green truck | vehicle | 
| 2   | 01.02.2017 00:00:00 | i have a blue rug | vehicle | 
| 3   | 01.02.2017 00:00:00 | i have a dark dog | NULL  | 
+--------------+---------------------+----------------------+----------+ 
+0

我看到,得到的結果適量,而是通過我的計算,PRODUCT_CODE 3不應該有一個類別,對不對?爲什麼它會在前三次獲得動物? – user2634997

+0

,因爲我失去了where子句,讓我編輯 – McNets

+0

謝謝,就是這樣! – user2634997

0

是的,你會得到這一個笛卡爾乘積。

您的問題是,您有多個與table1匹配的每個product_code的行。所以當你加入table3時,你會得到6個記錄,其ID爲1.其他連接條件並不構成雙方都有多個匹配的情況,所以你可以得到6個產品代碼1行2產品代碼2行和2個產品代碼3行。

解決方法是以外鍵在目標表中定位唯一行的方式進行連接。

這確實應該是一個有用的例子,爲什麼規範化和關鍵意識很重要。如果你打破了函數依賴的基本規則,那麼壞的問題就會增加。