my hosting company doesn't allow me to save query/view in mysql database (don't ask me ..why! i don't know), so i've to build an all in one nested query (that is difficult for my sql skills). My app is a simple "ticket managment system" Here the table structure ticket_master
ticket_id
assignedto_user
...
The other table, for users
userid
username
I've to get the count of ticket per user, starting from the USERS table to include ALL users (either who haven't ticket opened).
T开发者_开发问答he result must be
userid, username, tot_ticket
1, mr a, 3
2, mr b, Null (or zero)
3, mr c, 2
4, mr d, Null (or zero)
...
Thanks in advance!
Select U.userid, U.username
, Count(TM.ticket_id) As tot_ticket
From users As U
Left Join ticket_master As TM
On U.userid = TM.assignedto_user
Group By U.userid, U.username
SELECT
userid,
username,
COUNT(ticket_id)
FROM users
LEFT OUTER JOIN ticket_master on users.userid = ticket_master.assignedto_user
Not much different than @Thomas answer, except that I would avoid the grouping if your user list is already unique.
精彩评论