Skip to content
Advertisement

Query single row from join table and total count of joined table

I have two tables: accounts and users. users references accounts through account_id. What I want to do is get following query to work:

So, I’ll get following results

Basically, I want to get quantity of all users inside account and specific data for a single user. Is it even possible to do in a single query?

Advertisement

Answer

Assuming you want a single row for each user and account, you can do the following:

If you want data for any single user within the account, you can use get MAX(users.id) to get any user for an account and then fetch data for that user in the result.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement