简体   繁体   中英

How to replace a row from a table with another row from another table?

I have the following code on SQL:

select u.openid, u.screenname, svd.user_name
from gw_svd_prefix_assignment svd
join user_ u
on u.screenname = svd.USER_NAME;

now it will show three rows, screenname, user_name and openID. The screenname and user_name are the exact same, that's why I joined them, but I want to change the user_name to the value of openID which is different.

How can I do so?

EDIT Below is an example:

OPENID        SCREENNAME         USER_NAME
==========================================
Smith.A       Smith.Alan         Smith.Alan
Someone.J     Someone.Juan       Someone.Juan
Foo.V         Foo.Vallery        Foo.Vallery
Hee.L         Hee.Lee            Hee.Lee

I want the table to look like:

OPENID        SCREENNAME         USER_NAME
==========================================
Smith.A       Smith.Alan         Smith.A
Someone.J     Someone.Juan       Someone.J
Foo.V         Foo.Vallery        Foo.V
Hee.L         Hee.Lee            Hee.L

so I want to replace the values in User_Name with the corresponding ones from OPENID

尝试这个

select u.openid, u.screenname, u.openid user_name from gw_svd_prefix_assignment svd join user_ u on u.screenname = svd.USER_NAME;

If screenname is not unique in the user_ table, then the requirement doesn't make sense (which row in the user_ table should be used to update a row in svd , if the screenname is not unique?)

If screenname is unique, then the update can be done very easily as shown below, but this requires a UNIQUE constraint on screenname (or a Primary Key constraint). If a constraint doesn't exist currently, it can be added with an ALTER TABLE ... ADD CONSTRAINT ... statement.

update ( select user_name, openid, screenname 
           from svd join usr on svd.user_name = usr.screenname )
set user_name = openid;

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