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

后端 未结 5 1587
[愿得一人]
[愿得一人] 2020-12-24 13:29

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

相关标签:
5条回答
  • 2020-12-24 14:00

    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.

    0 讨论(0)
  • 2020-12-24 14:01

    I have used php or coldfusion to do something like this, (php example)

    function something($param){
    
       $localVarCleaned = mysql_real_escape_string($param);
    
       mysql_query("
       UPDATE tablename
       SET col = ".$localVarCleaned."
       ");
    }
    
    0 讨论(0)
  • 2020-12-24 14:05

    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);
    

    Hope that helps.

    0 讨论(0)
  • 2020-12-24 14:07

    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;

    0 讨论(0)
  • 2020-12-24 14:20

    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
    
    0 讨论(0)
提交回复
热议问题