简体   繁体   English

MySQL 在更新查询中设置局部变量

[英]MySQL Setting local variable inside update query

I have the following query inside a stored procedure:我在存储过程中有以下查询:

SET @myVar = '';

UPDATE `my_table` t1
SET 
    t1.field1 = CASE
        WHEN
            -- set @myVar to 'one' when this exists is true
            EXISTS (
                SELECT 1 
                FROM `table2` t2
                WHERE t1.field1 = t2.field1
            ) OR
           -- set @myVar to 'two' when this exists is true 
            EXISTS (
                SELECT 1
                FROM `table3` t3
                WHERE t1.field1 = t3.field1
            )
            THEN FALSE
        ELSE
            TRUE
    END,
    t1.field3 = CASE
        WHEN
            @myVar = 'one'
            THEN 'okay'
        WHEN
            @myVar = 'two'
            THEN 'not okay'
        ELSE
            'netural'
    END
WHERE t1.field5 = 'value';

Is there a way I can set some flag value ('one', 'two', etc...) in a local variable when first or second EXISTS condition is true in the CASE clause for first set .当第一个或第二个EXISTS条件在第一个setCASE子句中为真时,有没有办法可以在局部变量中设置一些标志值(“一个”、“两个”等...)。 Then I want to use that flag value to set another field.然后我想使用该标志值来设置另一个字段。

Is something like this possible?这样的事情可能吗? I have searched for while but can't seem to get a sufficient solution to this issue.我已经搜索了一段时间,但似乎无法获得足够的解决方案来解决这个问题。

EDIT : Added comments for where the session/local var should change.编辑:为会话/本地变量应更改的位置添加了注释。

This select query gives you an 1 as answer and sets @myvar to one此选择查询为您提供 1 作为答案并将@myvar 设置为 1

SELECT IF(IF(EXISTS (SELECT 1 FROM `df` t3 WHERE 1 = 1), @myVar :='one',0) = 'one',1,0);
SELECT @myVar;       

So try所以试试

UPDATE `my_table` t1
SET 
    t1.field1 = CASE
        WHEN
            IF(IF(            
            EXISTS (
                SELECT 1 
                FROM `table2` t2
                WHERE t1.field1 = t2.field1
            ), @myVar :='one',0) = 'one',1,0) OR
           IF(IF( 
            EXISTS (
                SELECT 1
                FROM `table3` t3
                WHERE t1.field1 = t3.field1
            ), @myVar :='two',0) = 'one',1,0)
            THEN FALSE
        ELSE
            TRUE
    END,
    t1.field3 = CASE
        WHEN
            @myVar = 'one'
            THEN 'okay'
        WHEN
            @myVar = 'two'
            THEN 'not okay'
        ELSE
            'netural'
    END
WHERE t1.field5 = 'value';

It doesn't give an error, but without data, you must test it yourself不会报错,但是没有数据就得自己测试了

And i am not sure if the session variable has an vqalue.而且我不确定会话变量是否有 vqalue。 like i said no data.就像我说没有数据。

But still set the session varoable before hand, it doewsn't make sense doiung it in the update statement.但是仍然事先设置会话变量,在更新语句中这样做没有意义。

There's no way to be certain of the successful completion of this query using variables the way you want to as MySQL does not guarantee the ordering of SET evaluation in an UPDATE query ("generally" != "guaranteed").没有办法以您想要的方式使用变量来确定此查询是否成功完成,因为 MySQL 不保证UPDATE查询中SET评估的顺序(“一般”!=“保证”)。 The safest solution is to repeat your EXISTS clauses:最安全的解决方案是重复您的EXISTS子句:

UPDATE `my_table` t1
SET 
    t1.field1 = CASE
        WHEN
            EXISTS (
                SELECT 1 
                FROM `table2` t2
                WHERE t1.field1 = t2.field1
            ) OR
            EXISTS (
                SELECT 1
                FROM `table3` t3
                WHERE t1.field1 = t3.field1
            )
            THEN FALSE
        ELSE
            TRUE
    END,
    t1.field3 = CASE
        WHEN
            EXISTS (
                SELECT 1 
                FROM `table2` t2
                WHERE t1.field1 = t2.field1
            )
            THEN 'okay'
        WHEN
            EXISTS (
                SELECT 1
                FROM `table3` t3
                WHERE t1.field1 = t3.field1
            )
            THEN 'not okay'
        ELSE
            'netural'
    END
WHERE t1.field5 = 'value';

The optimiser should be smart enough to realise that the subqueries are being used more than once and re-use the result.优化器应该足够聪明,能够意识到子查询被多次使用并重新使用结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM