pg.ddx.io pgsql-sql@postgresql.org mailing list archive
help / color / mirror / Atom feedHelp with a complex Update
3+ messages / 3 participants
[nested] [flat]
* Help with a complex Update
@ 2001-11-02 23:15 Orion <o2@trustcommerce.com>
0 siblings, 2 replies; 3+ messages in thread
From: Orion @ 2001-11-02 23:15 UTC (permalink / raw)
To: pgsql-sql
I have a table of users and a table of user_actions.
I'd like to have a first_action column in the user table.
How would I go about updating that info.
I'd immagine it'd be something like this:
update user_info set first_action = (select min(action_timestamp) from
action_info where user_id = ?? current user id ??);
Is this possible or do I have to make an external for loop?
Orion
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: Help with a complex Update
@ 2001-11-05 15:26 Ross J. Reedstrom <reedstrm@rice.edu>
parent: Orion <o2@trustcommerce.com>
1 sibling, 0 replies; 3+ messages in thread
From: Ross J. Reedstrom @ 2001-11-05 15:26 UTC (permalink / raw)
To: pgsql-sql
On Fri, Nov 02, 2001 at 03:15:36PM -0800, Orion wrote:
> I have a table of users and a table of user_actions.
> I'd like to have a first_action column in the user table.
>
> How would I go about updating that info.
>
> I'd immagine it'd be something like this:
>
> update user_info set first_action = (select min(action_timestamp) from
> action_info where user_id = ?? current user id ??);
Guessing at your table schema, but I think you want:
update user_info set first_action = min(action_timestamp) from
action_info where action_info.user_id = user_id;
Ross
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: Help with a complex Update
@ 2001-11-05 16:57 Josh Berkus <josh@agliodbs.com>
parent: Orion <o2@trustcommerce.com>
1 sibling, 0 replies; 3+ messages in thread
From: Josh Berkus @ 2001-11-05 16:57 UTC (permalink / raw)
To: o2@trustcommerce.com; pgsql-sql
Orion,
> update user_info set first_action = (select min(action_timestamp)
> from
> action_info where user_id = ?? current user id ??);
Close. You need to use a subselect in the FROM clause:
UPDATE user_info SET first_action = min_action
FROM (select user_id, min(action_timestamp) as min_action
FROM action_info WHERE user_id = $user_variable
GROUP BY user_id) ma
WHERE user_info.user_id = ma.user_id;
-Josh
______AGLIO DATABASE SOLUTIONS___________________________
Josh Berkus
Complete information technology josh@agliodbs.com
and data management solutions (415) 565-7293
for law firms, small businesses fax 621-2533
and non-profit organizations. San Francisco
^ permalink raw reply [nested|flat] 3+ messages in thread
end of thread, other threads:[~2001-11-05 16:57 UTC | newest]
Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2001-11-02 23:15 Help with a complex Update Orion <o2@trustcommerce.com>
2001-11-05 15:26 ` Ross J. Reedstrom <reedstrm@rice.edu>
2001-11-05 16:57 ` Josh Berkus <josh@agliodbs.com>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox