Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

sql - MySql How to set a local variable in an update statement (Syntax?)

How can I set a variable while doing an Update statement? I can't seem to figure out the syntax.

So I want something like this below but it's saying the syntax is wrong:

SET @tempVariable := 0;
UPDATE myTable SET col1 = 5, col2 = @tempVariable, @tempVariable := 100;
See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

This is possible :-

 UPDATE myTable SET col1 = 5,
 col2 = (@tempVariable:=@tempVariable+1) // to increment

To set an integer (not increment)

 UPDATE myTable SET col1 = 5, 
 col2 = (@tempVariable:=100) // to assign any integer

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...