简体   繁体   中英

Updating a column based on a condition in another column in the same table

Let me start by admitting that this is probably not the best engineering, but I am having the following question/problem.

I want to add values to column 'gc_stand'. I have data which connects the 'gc_stand' to 'startnummer' (ea (5 , 145) (78 , 2) (125 , 98) etc).

So my question is how to update the 'gc_stand' column without having to enter the values manually (around 200 values), but based on the connection between gc_stand and startnummer . I have inserted the data for the first two columns ( startnummer and rit_uitslag ) the same way (insert instead of update).

I am thinking about something like:

update etappe_4
    set gc_stand = ??
where startnummer = 'startnummer'

But where should i input my connected values then?

I have inserted the values by:

INSERT INTO etappe_1 (startnummer, rit_uitslag)
    VALUES (1,5), (2,145), (3,32) etc etc

And now I want to add the column (gc_stand). It is not possible by inserting, because it would create new rows. So therefore i guess i have to use UPDATE. But how?

It's a bit hard to make out what you are after, but I think you are looking for something like this:

update etappe_4
    set gc_stand = etappe_1.rit_uitslag
from etappe_1 
  where etappe_1.startnummer = etappe_4.startnummer

Note that this will only work properly if startnummer is unique in both tables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM