I am currently using this MySQL query:
$query="SELECT user, COUNT(*) AS num FROM tracks GROUP BY user ORDER BY COUNT(*) DESC";
which gives me a unique row for each user with a count of the number of entries for each user in the table ‘tracks’.
Not all users have an entry in the table ‘tracks’ though, and I would like to select/list all users on a page. All users are however included in another MySQL table called ‘users’.
Because I would like to display all users sorted by the count of entries in the ‘tracks’ table (with users with no entries in the ‘tracks’ table at the bottom), I presume that I need to use one SELECT query to achieve this, but I am not sure how to do this, and would be glad of some help.
You need to do a left join. Assuming you have a “users” table with a “user_id” column which matches the “user” column in the “tracks” table: