Re: query help request [2x]

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Andrei Ivanov <andrei(dot)ivanov(at)ines(dot)ro>
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: query help request [2x]
Date: 2003-04-01 19:25:41
Message-ID: 2421.1049225141@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

Andrei Ivanov <andrei(dot)ivanov(at)ines(dot)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

In response to

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message Andrei Ivanov 2003-04-01 19:34:49 Re: query help request [2x]
Previous Message Andrei Ivanov 2003-04-01 19:08:35 query help request [2x]