MYSQL - UPDATE multiple fields based on single CASE statement

时间:2018-02-01 18:39:13

标签: mysql sql sql-update case

OBJECTIVE

I am looking for a way to update one or both fields using the same CASE statement

UPDATE vendor
    SET special_cost =
    (
        CASE
            WHEN
                cost > 14
            THEN
                14
            ELSE
                SET special_cost = cost, cost = 14
        END
    )    
WHERE upc = '12345678912345'

LOGIC

This is some logic that explains what I want to happen above.

if ($cost > 14) {
    $special_cost = 14;
} else {
    $special_cost = $cost;
    $cost = 14;
}

It does not seem that there is a way to do this with a single CASE statement. Perhaps, this can be done with the mysql IF statement?

1 个答案:

答案 0 :(得分:2)

您指的是case 表达式而不是case 声明

你似乎想要这样的东西:

UPDATE vendor
    SET special_cost = GREATEST(cost, 14),
        cost = 14
    WHERE upc = '12345678912345';
相关问题