Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

TeeJay (2309)

TeeJay
  (email not shown publicly)
http://www.aarontrevena.co.uk/

Working in Truro
Graduate with BSc (Hons) in Computer Systems and Networks
pm : london.pm, bath.pm, devoncornwall.pm
lug : Devon & Cornwall LUG
CPAN : TEEJAY [cpan.org]
irc : TeeJay
skype : hashbangperl
livejournal : hashbangperl [livejournal.com]
flickr :hashbangperl [flickr.com]

Journal of TeeJay (2309)

Tuesday April 18, 2006
06:11 AM

more postgres suckage

[ #29360 ]

So I'm trying to update table A from values in table B using a join. Should be simple right..

update foo, bar set foo.col = bar.col where foo.bar = bar.id

works in mysql and most real databases
but postgres requires you jump through hoops and still screws it up

update task set category = c.category from task t, command c where t.command = c.id;

doesn't set category in task to be category from the matching command, it sets them all to the first category it meets

FFS!

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • I always thought that update with a join was a bit strange. Maybe you can rewrite this as a subselect.
    • update foo set col = bar.col where bar = bar.id

      you are't updating "bar" so why would you type "update foo, *bar*"? from comparisons with "select" it's a little odd that postgresql doesn't allow you to specify the table name of the table to be updated (the bare "col" and "bar" in my example).

      in your second command, you have three tables: task, command, and t (a second copy of task). you haven't specified how task and t are related.
  • Try this solution, I think it will do what you need:

    fred=> select * from task;

    command | category
    ---------+----------
    1       | old_cat1
    2       | old_cat2
    (2 rows)

    fred=> select * from command;
    category | id
    ----------+----
    cat1     |  1
    cat2     |  2
    (2 rows)

    fred=> update task set category = c.category FROM command c WHERE task.command=c
    .id;
    UPDATE 2
    fred=> select * from command;
    category | id
    ----------+----
    cat1