I have a table relationship which links one person to many relatives. so the tables are 1. Client. 2. Client_relative. I want to display all the rows of t开发者_StackOverflow中文版he Persons table, while displaying a count of how many relatives each person has. I have this query:
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
This isn't working. Any ideas?
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
精彩评论