Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
185 views
in Technique[技术] by (71.8m points)

mysql - SQL select and update

So I want to make an SQL query that returns a set of rows and also sets a certain column in those rows to null. So if I had a table like

column1 column2 column3
a       b       c
a       b       d
e       f       g

I would want to do the pseudo-SQL SELECT * FROM table WHERE column1 = a AND ALSO SET column2 = null and have that return my first two rows as they are above, while leaving the table looking like

column1 column2 column3
a       null    c
a       null    d
e       f       g

Can I do this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
DECLARE @column1 varchar(2);
SET @column1 = (SELECT distinct column1 FROM table WHERE column1 = 'a' );

select * from table where column1 = @column1;
update table set column2 = null where column1 = @column1;

this work for me in sql server..which will return the row as it is earlier and will update the column with new values in background


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...