From: | Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> |
---|---|
To: | Scott Frankel <leknarf(at)pacbell(dot)net> |
Cc: | pgsql-general(at)postgresql(dot)org |
Subject: | Re: sub-query question |
Date: | 2004-11-12 18:23:20 |
Message-ID: | 21110.1100283800@sss.pgh.pa.us |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
Scott Frankel <leknarf(at)pacbell(dot)net> writes:
> Here's my query:
> SELECT (
> u.color_id = (
> SELECT c.color_name
> FROM colors c
> WHERE color_id = 1)) AS color_name,
> u.name, u.the_date
> FROM users u
> WHERE u.color_id = 1
> ORDER BY u.the_date DESC LIMIT 1;
I think you want
SELECT
(SELECT c.color_name
FROM colors c
WHERE color_id = u.color_id) AS color_name,
u.name, u.the_date
FROM users u
WHERE u.color_id = 1
ORDER BY u.the_date DESC LIMIT 1;
The sub-select can refer to a variable of the outer query, as long
as you are careful to qualify it so it can't be mistaken for a variable
of the sub-select itself.
You could also express this query as a join. If you are pulling a whole
lot of users rows, the join way would probably be more efficient.
regards, tom lane
From | Date | Subject | |
---|---|---|---|
Next Message | Michael Fuhr | 2004-11-12 18:26:14 | Re: sub-query question |
Previous Message | Franco Bruno Borghesi | 2004-11-12 18:22:08 | Re: sub-query question |