How do I use a MySQL subquery to count the number of rows in a foreign table?

Posted by James Skidmore on Stack Overflow See other posts from Stack Overflow or by James Skidmore
Published on 2010-05-11T16:26:59Z Indexed on 2010/05/11 16:34 UTC
Read the original article Hit count: 196

Filed under:
|
|

I have two tables, users and reports. Each user has no, one, or multiple reports associated with it, and the reports table has a user_id field.

I have the following query, and I need to add to each row a count of how many reports the user has:

SELECT *
FROM users
LIMIT 1, 10

Do I need to use a subquery, and if so, how can I use it efficently? The reports table has thousands and thousands of rows.

© Stack Overflow or respective owner

Related posts about mysql

Related posts about php