简体   繁体   中英

Set SQL column in table A based on column in table B

I just can't quite get this syntax correct. Table 'clients' has a points_per_month column. Table 'usage' has an 'available' column and a 'client_id' column. I basically want to rewrite the available value to match the points_per_month value. I tried this:

update usage
set available = c.points_per_month
from usage u
inner join clients c on u.client_id = c.ident;

But that's setting all of the available values to the exact same number (the points_per_month of the first client returned)

Try something like

update usage u
set available = c.points_per_month
from clients c 
where u.client_id = c.ident;

The classic way with a subselect as assignment should work for every database:

update usage
set available = (select c.points_per_month
                   from clients c
                  where usage.client_id = c.ident
);

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