Lianthe Lianthe - 9 months ago 25
MySQL Question

How do i choose set of information from a table using information from another table?

I have a table called 'users' and a table called 'posts'. posts table holds id information for users as a foreign key attribute. I want to select every username that corresponds to id number in posts table FROM users table. Then i will use

function and
GROUP BY username
statement to be able to get post count for every user.
How can i do this?


I wouldn't group by username, because there could be multiple users having the same name. Instead group by the user's id.

SELECT users.username, count(*)
  FROM users, posts
  WHERE = posts.user_id