I have 2 tables in MySQL:
Persons
|ID | Name | Address
---------------------------------
| 1 | Someone | Somewhere
| 2 | Person2 | Somewhere else
And the table ActivePages
|ID | PersonID | Page
---------------------------------------
| 1 | 1 | somepage
| 1 | 1 | someotherpage
Now I need a query to return the following:
|PersonID | Count
------------------
|1 | 开发者_C百科2
|2 | 0
------------------
Who can help me with this? I think its straight forward but I keep getting wrong values...
Thanks in advance!
SELECT p.ID AS PersonID,
COUNT(a.PersonID) AS `Count`
FROM Persons p
LEFT JOIN ActivePages a ON a.PersonID = p.ID
GROUP BY p.ID
select personid,count(personid) as count from activepages
right join Persons on ActivePages.PersonID = Persons.ID group by personid
精彩评论