Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

syntax

sql

mysql

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;
like image 715
Ray Avatar asked Dec 12 '11 13:12

Ray


People also ask

What is the syntax for UPDATE command in MySQL?

MySQL Update Command Syntax The basic syntax of the Update query in MySQL is as shown below. UPDATE `table_name` is the command that tells MySQL to update the data in a table . SET `column_name` = `new_value' are the names and values of the fields to be affected by the update query.

How do I assign a variable in MySQL?

MySQL variable assignment There are two ways to assign a value to a user-defined variable. You can use either := or = as the assignment operator in the SET statement. For example, the statement assigns number 100 to the variable @counter. The second way to assign a value to a variable is to use the SELECT statement.

What does := mean in MySQL?

Description. := Assign a value. = Assign a value (as part of a SET statement, or as part of the SET clause in an UPDATE statement)

How do I assign a variable to another variable in MySQL?

MySQL variable assignment. There are two ways to assign a value to a user-defined variable. The first way is to use the SET statement as follows: SET @variable_name := value; You can use either := or = as the assignment operator in the SET statement. For example, the statement assigns number 100 to the variable @counter.

What is the syntax for update in MySQL?

The syntax starts with the keyword “UPDATE”, thereby informing the MySQL Server about the type of activity to be performed. This is a mandatory keyword and cannot be omitted. Next comes the name of the table on which the update action has to be performed. This is mandatory and cannot be omitted. Third, is again a keyword – SET.

How to use MySQL update to modify values in multiple columns?

Using MySQL UPDATE to modify values in multiple columns. To update values in the multiple columns, you need to specify the assignments in the SET clause. For example, the following statement updates both last name and email columns of employee number 1056:

What are system variables in mymysql?

MySQL system variables holds global or session level values, these variables are used to configure various operations. You can set values to these variables dynamically using the SET statement.


4 Answers

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
like image 200
ajreal Avatar answered Oct 23 '22 23:10

ajreal


If you want to obtain something like this:

SET @tempVariable := 0; UPDATE myTable SET col1 = 5, col2 = @tempVariable, @tempVariable := 100;

You can do a trick like this:

  • Create a column value.

ALTER TABLE Proj ADD col3 numeric;

  • Give a value to col3 in order to set the variable you need (@tempVariable).

SET @tempVariable := 0; UPDATE myTable SET col1 = 5, col2 = @tempVariable, col3 = @tempVariable := 100;

  • Drop the col3

ALTER TABLE Proj DROP col3;

In this way, you can assign values to a variable without change attributes of a table. It is really usefull when setting dinamic values.

FOR EXAMPLE: @tempVariable := @otherVariable + 100;

like image 33
kiquenet85 Avatar answered Oct 24 '22 00:10

kiquenet85


The key is the ":=" operators. MySQL User Variable

You can also assign a value to a user variable in statements other than SET. In this case, the assignment operator must be := and not = because the latter is treated as the comparison operator = in non-SET statements:

1 Use the one of the updating column

SET @tempVariable := 0;

UPDATE myTable 
SET col1 = 5, 
    col2 = @tempVariable := 100, 
    col3 = @tempVariable := col2 + 1;

@tempVariable is always 100 and col3 will be always 101. Seems mySQL will use the new assigned value instead of original value in the table. This is different from MS SQL. To make it more clear, try the following example, the value will be 1001 for col3 and @tempVariable.

UPDATE myTable 
SET col1 = 5, 
    col2 = @tempVariable := 100, 
    col2 = 1000
    col3 = @tempVariable := col2 + 1;

2 Use other column in the table than the updating column.

UPDATE myTable 
SET col1 = 5, 
    col2 = @tempVariable := 100, 
    col3 = @tempVariable := col4 + 1;

@tempVariable and col3 will have the same value. They will be the col4 original value + 1.

like image 44
Shen liang Avatar answered Oct 23 '22 22:10

Shen liang


I tested a similar query using a select and it worked for me, so I would rewrite your query as follows

SET @tempVariable := 0;
UPDATE myTable SET col1 = 5, col2 = (SELECT @tempVariable + 100);
like image 29
nightsinwhiteaustin Avatar answered Oct 23 '22 22:10

nightsinwhiteaustin