I have a two tables accounts and subs.
The first table has id and the second table has fields id, requester_account_id, grabber_account_id
I want the count of how many sub requests an account made, and how many grabs he did (based on if the requester_account_id/grabber_account_id is populated with his id)
In one query
output:
+------------+----------------+-----------------+
| account_id | subs_requested | subs_grabbed |
+------------+----------------+-----------------+
| 1 | 4 | 3 |
| 3 | 2 | 1 |
+------------+----------------+-----------------+
Use something like
The trick is to use table
substwice:subs as s1andsubs as s2, every time joined by different field….Note regarding efficiency: I’m not sure but I believe this solution is faster than the subquery solution, not tested it though (at least it won’t be slower). I always prefer
left joinover subquery whenever possible.