1

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?

2
  • 2
    What is the logic supposed to be doing? It doesn't make sense to me. Sample data and desired results help. Commented Feb 1, 2018 at 18:42
  • @GordonLinoff I updated the question to reflect the intended logic Commented Feb 1, 2018 at 18:47

1 Answer 1

2

You are referring to case expressions not case statements.

You appear to want something like this:

UPDATE vendor
    SET special_cost = GREATEST(cost, 14),
        cost = 14
    WHERE upc = '12345678912345';
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.