Re: query help request [2x] - Mailing list pgsql-novice

From Andrei Ivanov
Subject Re: query help request [2x]
Date
Msg-id Pine.LNX.4.50L0.0304012232100.2309-100000@webdev.ines.ro
Whole thread Raw
In response to Re: query help request [2x]  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-novice
Thanks.
Anyway, my query works, I've tested it.. I just didn't like its lack of
elegance... :)

On Tue, 1 Apr 2003, Tom Lane wrote:

> 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 by date:

Previous
From: Tom Lane
Date:
Subject: Re: query help request [2x]
Next
From: Doug Silver
Date:
Subject: errors using COPY from MySQL data