zermy
zermy

Reputation: 621

I need to update an id field for a table, without touching any other fields

I have a table called _field. Now, for some reason, a field inside the table called block got messy and now contains the wrong block numbers. I have the default data for _field, but some of the other fields can no longer be at default because of foreign constraints and stuff, so, I can't just restore the default data. So, for now, I created a new table called tmp_field and populated it with the default _field data.

Now, I need a query that takes the data from _field and changes all of the block numbers in _field to match the block numbers from tmp_field.

Another way to phrase it would be:
It checks each row of _field and if some field (like fieldid or name) matches the same field in tmp_field, then it changes the value of block in _field to match the value of block in tmp_field.

Note: I am using Postgresql 9.1.

Thanks for everything.

Upvotes: 0

Views: 75

Answers (1)

Joe Stefanelli
Joe Stefanelli

Reputation: 135858

UPDATE _field
    SET block = tmp_field.block
    FROM tmp_field
    WHERE _field.fieldid = tmp_field.fieldid
        OR _field.name = tmp_field.name

Upvotes: 2

Related Questions