我正在尝试从所有东西中选择所有内容,并计算更多资料中的东西id = morestuff id。
select *,
COUNT(morestuff.items) as total
from stuff,
morestuff
where stuff.id = '{$id}'
and morestuff.id = stuff.id
显然我的查询有问题,有人可以帮忙吗?
答案 0 :(得分:3)
SELECT s.*, coalesce(ms.Count, 0) as Count
FROM stuff s
left outer join (
select id, count(*) as Count
from morestuff
group by id
) ms on s.id = ms.id
WHERE s.id='{$id}'
答案 1 :(得分:3)
这可能是另一种选择:
select
*, (
select count(*)
from morestuff
where morestuff.id = stuff.id
) as total
from stuff
where id = '{$id}'