Good time of day. The question has ripened, and I want to solve it more elegantly.
There are 2 tables ( a and b ):
a.id | a.name b.id | b.aid | b.use ------+-------- ------+-------+------- 1 | Petya 1 | 1 | 0 2 | Vanya 2 | 2 | 1 3 | 1 | 0 4 | 1 | 1 5 | 2 | 0 6 | 1 | 1 7 | 1 | 0 At the exit you need to get:
a.id | a.name | count_total | count_busy ------+--------+-------------+------------ 1 | Petya | 5 | 2 2 | Vanya | 2 | 1 Where:
count_total- the number of elements ca.id = b.aid.count_busy- the number of elements ca.id = b.aid и b.use = 1
Is it possible to perform this task in one request? How to do it better?
- For @Yaant : corrected typo:
3by2. - For @Yura Ivanov : I haven't tried it in practice yet, but the first thing that comes to mind is nested queries.