From: | "Mel Jamero" <mel(at)gmanmi(dot)tv> |
---|---|
To: | "'Josh Berkus'" <josh(at)agliodbs(dot)com>, <pgsql-novice(at)postgresql(dot)org> |
Subject: | Re: ERROR: More than one tuple returned by a subselect used as an expression. |
Date: | 2003-04-04 01:04:08 |
Message-ID: | 000001c2fa46$18e598f0$1b06a8c0@mel |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-novice |
Thanks a lot Josh!!
I wasn't thinking too hard..
but then again the 2nd option (UPDATE..SET..FROM) you gave is really
something new to me. =)
-----Original Message-----
From: pgsql-novice-owner(at)postgresql(dot)org
[mailto:pgsql-novice-owner(at)postgresql(dot)org]On Behalf Of Josh Berkus
Sent: Friday, April 04, 2003 12:45 AM
To: mel(at)GMANMI(dot)TV; pgsql-novice(at)postgresql(dot)org
Subject: Re: [NOVICE] ERROR: More than one tuple returned by a subselect
used as an expression.
Mel,
> query is equivalent to "update table1 set field6 = (select table2_field2
> from table2 where table2_field5 = table1.field5)"
> my question is, how do i reformulate my SQL so that i can update table1
> such that it only gets the first occurrence of table2_field5 on table2 and
> ignore all the other occurrences? is there even a way where only 1 SQL
> statement is sufficient to carry out the desired result(s)?
There are a couple of ways. What do you mean by "first occurance"? First
chronologically, in primary key order, alphabetical, or something else?
UPDATE table1 SET field6 = (SELECT table2_field2
FROM table2 WHERE table2_field5 = table1.field5
ORDER BY table2_field9 LIMIT 1);
Or:
UPDATE table1 SET field6 = field2_min
FROM (SELECT field5, min(field2) as field2_min
FROM table2 GROUP BY field5) t2
WHERE t2.field5 = table1.field5;
Which is better depends on the orginization of your data/tables as well as
what you mean by "first".
--
Josh Berkus
Aglio Database Solutions
San Francisco
---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to majordomo(at)postgresql(dot)org
From | Date | Subject | |
---|---|---|---|
Next Message | pat pat | 2003-04-04 01:38:30 | Re: question about auto increment like |
Previous Message | Josh Berkus | 2003-04-04 00:55:31 | Re: ERROR: More than one tuple returned by a subselect used as an expression. |