Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从第一个表中获取结果,即使在第二个表-MySQL中没有找到匹配项_Mysql_Select_Left Join - Fatal编程技术网

从第一个表中获取结果,即使在第二个表-MySQL中没有找到匹配项

从第一个表中获取结果,即使在第二个表-MySQL中没有找到匹配项,mysql,select,left-join,Mysql,Select,Left Join,我有两张桌子,结构如下 Table name = counter ref_id INT NOT NULL count INT NOT NULL 我运行这个查询=>SELECT count,从计数器中休眠为c LEFT JOIN favs AS f ON c.ref\u id=f.discus\u id,其中ref\u id='post\u 5'和user\u id='1'。 但如果在第二个表favs中未找到匹配项,则它不返回任何行。我想要的是,如果找到结果,它应该返回两列,否则返回两列,第二

我有两张桌子,结构如下

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

我运行这个查询=>
SELECT count,从计数器中休眠为c LEFT JOIN favs AS f ON c.ref\u id=f.discus\u id,其中ref\u id='post\u 5'和user\u id='1'。

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


如何执行此操作?

用户在
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' 

用户在
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' 

伟大的谢谢@JW的快速回答太好了!谢谢@JW的快速回答