Displaying users in query result if they have no posts?

Jason

I have a bot which collects activity from certain users and logs posts they make on one of my websites. I have a list of moderators on this website and I would like to use MySQL to return information about their activity. At the moment, I do the following:

SELECT count( num ) AS posts, username
FROM `logs`
WHERE username
IN (
'username1', 'username2', 'username3', 'username4', 'username5', 
)
AND FROM_UNIXTIME( epoch )
BETWEEN "2015-05-26"
AND "2015-05-29"
GROUP BY username
ORDER BY posts DESC
LIMIT 0 , 30 

Some sample output from the above query:

username1 100
username2 50
username3 25

What I want to output: How do I do this?

username1 100
username2 50
username3 25
username4 0
username5 0

Table structure:

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

There's the IFNULL function that I think applies in this case

SELECT IFNULL(count( num ),0) AS posts, username
FROM `logs` ...

If the value of the count is null the posts will return 0


MySQL has some limitations that make queries like this particularly hard. What's hard is creating a temporary list of usernames that you need to join to make your query work

There is only one way that I know to do this. It involves storing the data in a temporary table and then querying with that table.

CREATE temporary table temp_users (name VARCHAR(30))

INSERT INTO temp_users (name) VALUES('username 1'),('username 2'),('username 3');

SELECT count( num ) AS posts, temp_username.username
FROM temp_username LEFT JOIN `logs` ON logs.username=temp_username.username
  AND FROM_UNIXTIME( epoch )
  BETWEEN "2015-05-26" AND "2015-05-29"
  GROUP BY username
  ORDER BY posts DESC
  LIMIT 0 , 30) 

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

The table is deleted at the end of the session

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Displaying users in query result if they have no posts?

From Dev

Show Users in a loop - but even if they have no posts

From Dev

Displaying posts of users the user follows through Laravel relationships

From Dev

Android - Parse query to get all posts and the users to go with the posts

From Dev

Query returning unfollowed users result

From Dev

Query returning unfollowed users result

From Dev

PHP not displaying result from MYSQL query

From Dev

python mysql query not displaying correct result

From Dev

mysql query result is not displaying some rows

From Dev

SQL query: how to get users and their three latest posts?

From Dev

ActiveRecord Query: Get Distinct Users from Posts in Category

From Dev

query schema to get posts from users that followed by someone

From Dev

Postgres Query: Ranking posts by users based on user activity

From Dev

query schema to get posts from users that followed by someone

From Dev

PHP loop not displaying posts

From Dev

Displaying Posts to profile in groups

From Dev

Displaying posts of a category wordpress

From Dev

Posts Displaying In Wrong Order

From Dev

Get all posts that have NO terms with WP_Query

From Dev

Get all posts that have NO terms with WP_Query

From Dev

meteor js: how to query for posts that have recently been rated?

From Dev

SQL Query only displaying first result rather than arrayed data

From Dev

Why does this query have only one result?

From Dev

mysql query displaying #1064 - You have an error in your SQL syntax;

From Dev

Display following users posts

From Dev

Physical directories for posts and users

From Dev

Wordpress displaying posts on different pages

From Dev

Displaying a User's Posts by Category

From Dev

Displaying Pagination in posts in new theme

Related Related

  1. 1

    Displaying users in query result if they have no posts?

  2. 2

    Show Users in a loop - but even if they have no posts

  3. 3

    Displaying posts of users the user follows through Laravel relationships

  4. 4

    Android - Parse query to get all posts and the users to go with the posts

  5. 5

    Query returning unfollowed users result

  6. 6

    Query returning unfollowed users result

  7. 7

    PHP not displaying result from MYSQL query

  8. 8

    python mysql query not displaying correct result

  9. 9

    mysql query result is not displaying some rows

  10. 10

    SQL query: how to get users and their three latest posts?

  11. 11

    ActiveRecord Query: Get Distinct Users from Posts in Category

  12. 12

    query schema to get posts from users that followed by someone

  13. 13

    Postgres Query: Ranking posts by users based on user activity

  14. 14

    query schema to get posts from users that followed by someone

  15. 15

    PHP loop not displaying posts

  16. 16

    Displaying Posts to profile in groups

  17. 17

    Displaying posts of a category wordpress

  18. 18

    Posts Displaying In Wrong Order

  19. 19

    Get all posts that have NO terms with WP_Query

  20. 20

    Get all posts that have NO terms with WP_Query

  21. 21

    meteor js: how to query for posts that have recently been rated?

  22. 22

    SQL Query only displaying first result rather than arrayed data

  23. 23

    Why does this query have only one result?

  24. 24

    mysql query displaying #1064 - You have an error in your SQL syntax;

  25. 25

    Display following users posts

  26. 26

    Physical directories for posts and users

  27. 27

    Wordpress displaying posts on different pages

  28. 28

    Displaying a User's Posts by Category

  29. 29

    Displaying Pagination in posts in new theme

HotTag

Archive