我有一个表关系,将一个人与许多亲戚联系起来。所以表是1.客户。 2. Client_relative。我想显示Persons表的所有行,同时显示每个人拥有多少亲戚的计数。我有这个问题:
SELECT c.clientid, c.fname, c.lname, count(cr.relativeid) as relativecount FROM {client} AS c INNER JOIN {client_relative} cr on c.clientid = cr.clientid
这不起作用。有任何想法吗?
答案 0 :(得分:1)
select c.*, cc.relativecount
from client c
inner join (
select clientid, count(*) as relativecount
from client_relative
group by clientid
) cc on c.clientid = cc.clientid