简体   繁体   English

NATURAL JOIN无法正常运作

[英]NATURAL JOIN does not work properly

I have three tables describes like that: 我有三个表描述像这样:

+----------+    +-----------------+    +----------+ 
| products |    | products_stores |    | stores   | 
+----------+    +-----------------+    +----------+ 
| barecode |    | #barecode       |    | storeID  | 
| name     |----| #storeID        |----| location | 
+----------+    | price           |    +----------+ 
                +-----------------+    

Created them like this: 像这样创建它们:

CREATE TABLE IF NOT EXISTS `products` (
  `barecode` varchar(100) NOT NULL UNIQUE,
  `name` varchar(25) NOT NULL,
  PRIMARY KEY (`barecode`)
);
CREATE TABLE IF NOT EXISTS `stores` (
  `idStore` int(10) NOT NULL AUTO_INCREMENT,
  `name` varchar(25) NOT NULL,
  `location` varchar(100) NOT NULL,
  PRIMARY KEY (`idStore`)
);
CREATE TABLE IF NOT EXISTS `products_stores` (
  `idStore` int(10) NOT NULL,
  `barecode` VARCHAR(100) NOT NULL,
  `price` double NOT NULL,
  FOREIGN KEY (`barecode`) REFERENCES `products`(`barecode`),
  FOREIGN KEY (`idStore`) REFERENCES `stores`(`idStore`)
);

I wanted to get all the product and its price with the right store so I tried NATURAL JOIN but it returned nothing (code below 1-). 我想在合适的商店获得所有产品及其价格,所以我尝试了NATURAL JOIN,但它什么也没返回(代码在1-以下)。 So I tried with only one NATURAL JOIN just to check and it works (code below 2- and 3-). 因此,我只尝试了一个NATURAL JOIN来检查它是否可以工作(代码在2-和3-下面)。

1- SELECT * FROM products NATURAL JOIN products_stores NATURAL JOIN stores;
2- SELECT * FROM products JOIN products_stores NATURAL JOIN stores;
3- SELECT * FROM products NATURAL JOIN products_stores JOIN stores;

I do not understand why the "double" NATURAL JOIN does not work. 我不明白为什么“双”自然联接不起作用。 Does anyone can help me through this? 有人可以帮助我吗? Thanks. 谢谢。

It depends on your data,here is what the documentation has to say about it 这取决于您的数据,这是文档必须说明的内容

The evaluation of multi-way natural joins differs in a very important way that affects the result of NATURAL or USING joins and that can require query rewriting. 多向自然联接的评估在一个非常重要的方面有所不同,这会影响NATURAL联接或USING联接的结果,并且可能需要重写查询。 Suppose that you have three tables t1(a,b), t2(c,b), and t3(a,c) that each have one row: t1(1,2), t2(10,2), and t3(7,10). 假设您有三个表t1(a,b),t2(c,b)和t3(a,c)各自具有一行:t1(1,2),t2(10,2)和t3( 7,10)。 Suppose also that you have this NATURAL JOIN on the three tables: 还假设您在三个表上都具有这个NATURAL JOIN:

SELECT ... FROM t1 NATURAL JOIN t2 NATURAL JOIN t3; 从t1自然联接t2自然联接t3中选择... Previously, the left operand of the second join was considered to be t2, whereas it should be the nested join (t1 NATURAL JOIN t2). 以前,第二个联接的左操作数被认为是t2,而它应该是嵌套联接(t1 NATURAL JOIN t2)。 As a result, the columns of t3 are checked for common columns only in t2, and, if t3 has common columns with t1, these columns are not used as equi-join columns. 结果,仅在t2中检查t3列中是否有公共列,并且,如果t3与t1具有公共列,则这些列将不用作等价连接列。 Thus, previously, the preceding query was transformed to the following equi-join: 因此,以前,先前的查询已转换为以下等值连接:

SELECT ... FROM t1, t2, t3 WHERE t1.b = t2.b AND t2.c = t3.c; 在t1,t2,t3中选择... ... t1.b = t2.b AND t2.c = t3.c; That join is missing one more equi-join predicate (t1.a = t3.a). 该联接缺少另一个等联接谓词(t1.a = t3.a)。 As a result, it produces one row, not the empty result that it should. 结果,它产生一行,而不是它应为空的结果。 The correct equivalent query is this: 正确的等效查询是这样的:

SELECT ... FROM t1, t2, t3 WHERE t1.b = t2.b AND t2.c = t3.c AND t1.a = t3.a; 从t1,t2,t3中选择... ... t1.b = t2.b AND t2.c = t3.c AND t1.a = t3.a;

In short the second JOIN only checks for the previous table`s column not the WHOLE result of the first JOIN. 简而言之,第二个JOIN仅检查上一个表的列,而不检查第一个JOIN的整个结果。

One of the reasons that NATURAL JOINs are not recommended,not much control and little to gain. 不推荐使用NATURAL JOIN的原因之一是控制不多,获得的收益也很少。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM