1

我有两张表,其结构如下

Table name = counter
ref_id INT NOT NULL
count INT NOT NULL

Table name = favs
disqus_id VARCHAR(32) NOT NULL
user_id INT NOT NULL
dormant VAHCHAR(10) NOT NULL

我运行这个查询 =>SELECT count, dormant FROM counter AS c LEFT JOIN favs AS f ON c.ref_id = f.disqus_id WHERE ref_id = 'post_5' AND user_id = '1'.

但如果在第二个表中找不到匹配项,它不会返回任何行favs。我想要的是,如果找到结果,它应该返回两列,否则返回两列,第二列是NULL

我该怎么做呢?

4

1 回答 1

1

ON子句中用户的条件

SELECT c.count, f.dormant 
FROM   counter AS c 
       LEFT JOIN favs AS f 
          ON c.ref_id = f.disqus_id  AND f.user_id = '1'
WHERE  c.ref_id = 'post_5' 

或者如果仍然不起作用,请移动两者:

SELECT c.count, f.dormant 
FROM   counter AS c 
       LEFT JOIN favs AS f 
          ON c.ref_id = f.disqus_id  AND 
             f.user_id = '1' AND
             c.ref_id = 'post_5' 
于 2013-02-08T19:03:40.950 回答