简体   繁体   English

SQL LEFT JOIN和WHERE产生意外结果

[英]SQL LEFT JOIN and WHERE yields unexpected result

I've got two tables related by a field called 'status'. 我有两个与名为“状态”的字段相关的表。 With table2.location having a value of 'texas', there are 20 instances of table2.status having a value of 'good'. 如果table2.location的值为“ texas”,则有20个table2.status实例的值为“ good”。

That is, the following query returns 20 rows of table2.status = 'good'. 也就是说,以下查询返回table2.status ='good'的20行。

[A] SELECT table2.status FROM table2 WHERE table2.location = 'texas';

Further, there are 50 unique table1.id's with table1.status = 'good'. 此外,还有50个唯一的table1.id,其中table1.status ='good'。

That is, the following query returns 50 rows of unique table1.id's. 也就是说,以下查询返回50行唯一的table1.id。

[B] SELECT table1.id FROM table1 WHERE table1.status = 'good';

Now, when I run the following query: 现在,当我运行以下查询时:

[C] SELECT table1.id FROM table1 LEFT JOIN table2 ON table1.status = table2.status WHERE table2.location = "texas";

I would expect it to return the 50 rows of unique id's. 我希望它返回50行唯一ID。 However, it is actually returning the 50 rows of unique id's 20 times (ie. I get 1000 rows returned). 但是,它实际上是返回50行唯一ID的20次(即,我返回了1000行)。

The quick fix I did was to simply execute SELECT DISTINCT table1.id... which then just returns one set of 50 rows (not 20 sets of 50 rows). 我所做的快速修复是简单地执行SELECT DISTINCT table1.id ...,然后仅返回一组50行(而不是20组50行)。

However, I'd like to know why I'm seeing this behahvior - maybe there is something wrong with my query [C] ? 但是,我想知道为什么会看到这种行为-我的查询[C]可能有问题吗?

Thanks! 谢谢!

Your query is wrong if you want to get 50 items: you're performing a cross join 50x20, so you have 1000 records as result. 如果要获取50个项目,则您的查询是错误的:您正在执行50x20的交叉联接,因此您有1000条记录。
You cannot join on status (which is not unique): probably your table design is wrong. 您无法加入状态(这不是唯一的):可能您的表设计错误。
IMO you should have id in both tables and join with it... IMO,您应该在两个表中都具有id并将其加入...

it is expected. 这是预期的。 1st row from table 1 matches 50 rows from table 2, 2nd row from table 1 matches again the same 50 rows from table 2 etc. 表1的第一行匹配表2的50行,表1的第二行再次匹配表2的相同50行,依此类推。

Your query has two problems 您的查询有两个问题

SELECT table1.id 
FROM table1 
LEFT JOIN table2 ON table1.status = table2.status 
WHERE table2.location = "texas";

First when you usae a left join a condition on the second table must be in the ON clause not the where clause or you convert the left join to an inner join (Since the condition must be met) 首先,当您使用左联接时,第二个表上的条件必须位于ON子句中,而不是where子句中,或者您将左联接转换为内部联接(因为必须满足条件)

So your query should start looking like this: 因此,您的查询应开始如下所示:

SELECT table1.id 
FROM table1 
LEFT JOIN table2 
    ON table1.status = table2.status 
    AND table2.location = "texas";

Now your next problem is that status is unlikely to be the thing you actually want to join on. 现在,您的下一个问题是身份不太可能成为您真正想要加入的对象。 To help you get the results you want though, we would need to see the table structure of the two tables. 为了帮助您获得所需的结果,我们需要查看两个表的表结构。

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

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