Re: Dumb question about count()

Поиск
Список
Период
Сортировка
От Ezequiel Tolnay
Тема Re: Dumb question about count()
Дата
Msg-id dbqbqj$j0p$1@news.hub.org
обсуждение исходный текст
Ответ на Dumb question about count()  (Benjamin Smith <lists@benjamindsmith.com>)
Список pgsql-general
Benjamin Smith wrote:
> Now, I want to get a result like:
>
> classroom | students | seats
> 101A     0    25
> 101B    22    30
> 102A    11    0
> ... etc.
>
> Something somewhat akin to
>
> select classroom.title,
>     count(students.id) AS students,
>     count(seats.id) AS seats
> from classrooms, students, seats
> where classrooms.id=students.classrooms_id
> and classrooms.id=seats.id

try

select classroom.title,
    (select count(*) from students
     where classrooms_id = c.id) AS students,
    (select count(*) from seats
     where classrooms_id = c.id) AS students,
    count(seats.id) AS seats
from classrooms c

Cheers,

Eze

В списке pgsql-general по дате отправления:

Предыдущее
От: Ezequiel Tolnay
Дата:
Сообщение: Re: Wishlist?
Следующее
От: Tom Lane
Дата:
Сообщение: Re: Query planner refuses to use index