Re: query help request [2x]

Поиск
Список
Период
Сортировка
От Tom Lane
Тема Re: query help request [2x]
Дата
Msg-id 2421.1049225141@sss.pgh.pa.us
обсуждение исходный текст
Ответ на query help request [2x]  (Andrei Ivanov <andrei.ivanov@ines.ro>)
Ответы Re: query help request [2x]  (Andrei Ivanov <andrei.ivanov@ines.ro>)
Список pgsql-novice
Andrei Ivanov <andrei.ivanov@ines.ro> writes:
> Then I came up with this:

> SELECT u.id, u.nick, pr.keywords,
>   COALESCE((SELECT id FROM pictures WHERE user_id = u.id), 0) AS has_picture
> FROM users u JOIN profiles pr ON u.id = pr.user_id;

This will actually fail if any user has more than one picture.

I think you have to go with

SELECT u.id, u.nick, pr.keywords,
  EXISTS(SELECT 1 FROM pictures WHERE user_id = u.id) AS has_picture
FROM users u JOIN profiles pr ON u.id = pr.user_id;

This should perform reasonably well as long as there's an index on
pictures.user_id.

            regards, tom lane


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

Предыдущее
От: Andrei Ivanov
Дата:
Сообщение: query help request [2x]
Следующее
От: Andrei Ivanov
Дата:
Сообщение: Re: query help request [2x]