简体   繁体   English

为什么此SELECT…JOIN语句不返回结果?

[英]Why does this SELECT … JOIN statement return no results?

I have two tables: 我有两个表:
1. tableA is a list of records with many columns. 1. tableA是具有许多列的记录列表。 There is a timestamp column called "created" 有一个称为“创建”的时间戳列
2. tableB is used to track users in my application that have locked a record in tableA for review. 2. tableB用于跟踪应用程序中锁定了tableA中的记录以供查看的用户。 It consists of four columns: id, user_id, record_id, and another timestamp collumn. 它由四列组成:id,user_id,record_id和另一个时间戳列。

I'm trying to select up to 10 records from tableA that have not been locked by for review by anyone in tableB (I'm also filtering in the WHERE clause by a few other columns from tableA like record status). 我正在尝试从tableA中选择最多10条未被锁定的记录,以供tableB中的任何人查看(我还在WHERE子句中过滤了tableA中的其他几列,例如记录状态)。 Here's what I've come up with so far: 到目前为止,这是我想出的:

SELECT tableA.* FROM tableA
  LEFT OUTER JOIN tableB ON tableA.id = tableB.record_id WHERE
  tableB.id = NULL AND
  tableA.status = 'new' AND
  tableA.project != 'someproject' AND
  tableA.created BETWEEN '1999-01-01 00:00:00' AND '2010-05-06 23:59:59'
  ORDER BY tableA.created ASC LIMIT 0, 10;

There are currently a few thousand records in tableA and zero records in tableB. 当前,表A中有几千条记录,表B中有零条记录。 There are definitely records that fall between those timestamps, and I've verified this with a simple 在这些时间戳之间肯定有记录,我已经用一个简单的方法验证了这一点。

SELECT * FROM tableA WHERE  
created BETWEEN '1999-01-01 00:00:00' AND '2010-05-06 23:59:59'  

The first statement above returns zero rows, and the second one returns over 2,000 rows. 上面的第一条语句返回零行,第二条语句返回2,000行以上。

tableB.id = NULL 

should be 应该

tableB.id IS NULL 

It is never true as is (Nor is it false. NULL=NULL evaluates to unknown so tableB.id <> NULL would similarly not return any results). 它永远不会是真的(也不会是false。NULL= NULL的结果为未知,因此tableB.id <> NULL同样不会返回任何结果)。

See http://dev.mysql.com/doc/refman/5.0/en/working-with-null.html and Why does NULL = NULL evaluate to false in SQL server 参见http://dev.mysql.com/doc/refman/5.0/en/working-with-null.html,以及为什么NULL = NULL在SQL Server中评估为false

Do any of those records between the timestamps follow your other two guidelines, such as being new but not 'someproject'? 时间戳之间的任何记录是否都遵循其他两个准则,例如是新的但不是“ someproject”?

Also, any time you use NULL, you need to use IS or IS NOT, rather than =/!=. 另外,任何时候使用NULL时,都需要使用IS或IS NOT,而不是= /!=。

I don't remember if MySQL's structure is different on the idea of constraints, but if tableB.id is your primary key, than it cannot be NULL. 我不记得MySQL的结构在约束概念上是否有所不同,但是如果tableB.id是您的主键,那么它就不能为NULL。

Finally, if there aren't any records in Table B, how is it supposed to search the non-existent records? 最后,如果表B中没有任何记录,应该如何搜索不存在的记录? It will always return zero since there aren't any matching records in tableB and tableA since tableB is empty. 由于tableB和tableA中没有任何匹配的记录,因为tableB为空,它将始终返回零。

Try entering a dummy record in TableB that matches your search criteria and see if the output changes. 尝试在TableB中输入一个与您的搜索条件匹配的伪记录,然后查看输出是否更改。

Already answered, but here is some general troubleshooting advice: don't try to solve so many problems at once. 已经回答了,但是这里有一些常规的故障排除建议:不要试图一次解决那么多问题。 My first approach would have been to eliminate all of the filtering where conditions and try it. 我的第一种方法是消除所有过滤条件,然后尝试一下。 If I get data, then add half of them back in. If that works, try the other half. 如果我得到数据,则将其中一半重新添加。如果可行,请尝试另一半。 Repeat until the problem is staring you in the face. 重复进行直到问题一直困扰着您。 By troubleshooting this way, you'd have quickly narrowed it down to the "tableB.id = NULL" statement, and probably would have had a D'OH! 通过这种方式进行故障排除,您将很快将其范围缩小为“ tableB.id = NULL”语句,并且可能会有D'OH! moment and figured it out. 片刻,想通了。

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

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