Slash Boxes
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)

  (email not shown publicly)

Working in Truro
Graduate with BSc (Hons) in Computer Systems and Networks
pm :,,
lug : Devon & Cornwall LUG
irc : TeeJay
skype : hashbangperl
livejournal : hashbangperl []
flickr :hashbangperl []

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 =

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 =;

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


The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
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 =

      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
    UPDATE 2
    fred=> select * from command;
    category | id