PostgreSQL 3表加入乘法

时间:2017-02-17 17:23:51

标签: sql postgresql cartesian-product

我有3张桌子。第一个有我想要的记录。另外两个类别要应用于第一个表。如果在描述中找到了table3的查找值,我想返回该类别。否则,返回table2中的类别。我认为我的逻辑是正确的,但结果却成倍增加。如何将结果限制为我想要的table1记录,但应用正确的类别?

这是我的查询示例模式。它应该只返回table1中的前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 个答案:

答案 0 :(得分: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     |
+--------------+---------------------+----------------------+----------+

答案 1 :(得分:0)

是的,你会得到一个笛卡尔积。

您的问题是每个product_code的多行与table1匹配。所以当你加入table3时,你会得到6条id为1的记录。其他的连接条件不会造成双方都有多个匹配的情况,这就是你如何得到6个产品代码1行,2产品代码2行和2个产品代码3行。

解决方案是以外键的方式连接目标表中的唯一行。

这确实应该是规范化和关键意识重要的一个有用的例子。在你打破功能依赖的基本规则的地方,不良问题会成倍增加。