MySql子查询值未显示正确的结果

时间:2012-03-12 14:44:11

标签: mysql subquery

如果此account_id的列表中没有任何记录,我如何重写此查询以显示子查询claims的正确值,但claimed_listings中有1条记录。谢谢!

SELECT status, (SELECT count(id) 
               FROM claimed_listings 
               WHERE account_id = 1) AS claims  
FROM listings 
WHERE account_id = 1

我希望看到像

这样的结果
status | claims
     A | 1
     F | 1
     E | 1

在这种情况下,有三个列表和1个声明列表。 问题是如果没有列表和1个声明列表我根本没有结果?

2 个答案:

答案 0 :(得分:2)

试试这个:

SELECT a.Account_ID, Count(b.Account_ID) TotalAcount
FROM claimed_Listings a LEFT JOIN listings b
        on a.account_ID = b.Account_ID
WHERE a.Account_ID = 1
GROUP BY a.Account_ID

答案 1 :(得分:0)

尝试以下方法(希望我能帮到你......)

select count(cl.id), count(cl.account_id)
from claimed_listings cl
where cl.account_id=1 and cl.account_id not in (select account_id from listings)
group by cl.id
having count(cl.account_id) > 0;