开发者

Query to count values

开发者 https://www.devze.com 2023-01-27 11:57 出处:网络
I have 2 tables in MySQL: Persons ID| Name|Address --------------------------------- 1| Someone |Somewhere

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
0

精彩评论

暂无评论...
验证码 换一张
取 消