[英]How to get Count from Subquery when main query returns no records
The MYSQL query below gives me the output I need when there are records found in the main query eg 下面的MYSQL查询为我提供了在主查询中找到记录时所需的输出,例如
RETURN: 返回:
username | total_favs
this_user | 4
But I get nothing return if no records found in main query. 但如果在主查询中找不到记录,我什么都得不到。 I want to get something like this: 我想得到这样的东西:
RETURN: 返回:
username | total_favs
null | 4
SELECT
c.username,
(SELECT COUNT(*) AS total_records FROM favourites f WHERE f.pic_id = 177) AS `total_favs`
FROM
comments c
WHERE
c.pic_id = 177
Switch it around so favourites
is the main query: 切换它,所以favourites
是主要查询:
SELECT c.username, COUNT(*) AS total_favs
FROM favourites f
LEFT OUTER JOIN comments c on f.pic_id = c.pic_id
WHERE f.pic_id = 177
GROUP BY c.username
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.