mysql - Displaying users in query result if they have no posts? -


i have bot collects activity users , logs posts make on 1 of websites. have list of moderators on website , use mysql return information activity. @ moment, following:

select count( num ) posts, username `logs` username in ( 'username1', 'username2', 'username3', 'username4', 'username5',  ) , from_unixtime( epoch ) between "2015-05-26" , "2015-05-29" group username order posts desc limit 0 , 30  

some sample output above query:

username1 100 username2 50 username3 25 

what want output: how do this?

username1 100 username2 50 username3 25 username4 0 username5 0 

table structure:

num (int, key) username (varchar) epoch (varchar)  msg (varchar) 

there's ifnull function think applies in case

select ifnull(count( num ),0) posts, username `logs` ... 

if value of count null posts return 0


mysql has limitations make queries particularly hard. what's hard creating temporary list of usernames need join make query work

there 1 way know this. involves storing data in temporary table , querying table.

create temporary table temp_users (name varchar(30))  insert temp_users (name) values('username 1'),('username 2'),('username 3');  select count( num ) posts, temp_username.username temp_username left join `logs` on logs.username=temp_username.username   , from_unixtime( epoch )   between "2015-05-26" , "2015-05-29"   group username   order posts desc   limit 0 , 30)  

here's example that's been simplified http://sqlfiddle.com/#!9/cc4ef1/11/0

the table deleted @ end of session


Comments

Popular posts from this blog

Java 3D LWJGL collision -

methods - python can't use function in submodule -

c# - ErrorThe type or namespace name 'AxWMPLib' could not be found (are you missing a using directive or an assembly reference?) -