![](/img/trans.png)
[英]laravel get result from first table if second table did not match join condition
[英]Get the result from the first table even if no match is found in the second table - MySQL
我有兩個表,其結構如下
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
我該怎么做呢?
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'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.