How to declare a variable in MySQL?

后端 未结 7 1593
春和景丽
春和景丽 2020-11-22 05:01

How to declare a variable in mysql, so that my second query can use it?

I would like to write something like:

SET start = 1;
SET finish = 10;

SELECT         


        
7条回答
  •  广开言路
    2020-11-22 05:50

    SET

    SET @var_name = value 
    

    OR

    SET @var := value
    

    both operators = and := are accepted


    SELECT

    SELECT col1, @var_name := col2 from tb_name WHERE "conditon";
    

    if multiple record sets found only the last value in col2 is keep (override);

    SELECT col1, col2 INTO @var_name, col3 FROM .....
    

    in this case the result of select is not containing col2 values


    Ex both methods used

    -- TRIGGER_BEFORE_INSERT --- setting a column value from calculations

    ...
    SELECT count(*) INTO @NR FROM a_table WHERE a_condition;
    SET NEW.ord_col =  IFNULL( @NR, 0 ) + 1;
    ...
    

提交回复
热议问题