Lianthe Lianthe - 1 year ago 68
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?

Answer Source

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download