MySQL update case help

前端 未结 6 1482
隐瞒了意图╮
隐瞒了意图╮ 2020-11-27 18:58

Can anybody tell me how to fix this query?

update ae44
set Price = Case
when ID = \'AT259793380\' then \'500\'
when ID = \'AT271729590\' then \'600\'
when ID         


        
相关标签:
6条回答
  • 2020-11-27 19:23
    UPDATE ae44 SET
        Price = CASE
        WHEN ID = 'AT259793380' THEN '500'
        WHEN ID = 'AT271729590' THEN '600'
        WHEN ID = 'AT275981020' THEN '700'
        ELSE Price END
    

    Or you can use WHERE:

    UPDATE ae44 SET
        Price = CASE
        WHEN ID = 'AT259793380' THEN '500'
        WHEN ID = 'AT271729590' THEN '600'
        WHEN ID = 'AT275981020' THEN '700'
        END
    WHERE 
        ID IN ('AT259793380', 'AT271729590', 'AT275981020')
    

    And set the LIMIT is good idea too:

    UPDATE ae44 SET
        Price = CASE
        WHEN ID = 'AT259793380' THEN '500'
        WHEN ID = 'AT271729590' THEN '600'
        WHEN ID = 'AT275981020' THEN '700'
        END
    WHERE 
        ID IN ('AT259793380', 'AT271729590', 'AT275981020')
    LIMIT 3
    
    0 讨论(0)
  • 2020-11-27 19:28

    I'm assuming this is a mysql query. You can use the first query that Quassnoi posted and just add

    WHEN ID THEN price
    

    as the last "WHEN". This will prevent all of your price fields from being set to null

    UPDATE  ae44
        SET price =
        CASE
        WHEN ID = 'AT259793380' THEN '500'
        WHEN ID = 'AT271729590' THEN '600'
        WHEN ID = 'AT275981020' THEN '700'
        WHEN ID THEN price
        END
    
    0 讨论(0)
  • 2020-11-27 19:39
    UPDATE  ae44
    SET     price =
            CASE
            WHEN ID = 'AT259793380' THEN
                    '500'
            WHEN ID = 'AT271729590' THEN
                    '600'
            WHEN ID = 'AT275981020' THEN
                    '700'
            END
    

    Note than this query will update prices for the records with other ids to NULL.

    If you only want to update only ids from your list, use this:

    UPDATE  ae44
    JOIN    (
            SELECT  'AT259793380' AS oldval, '500' AS newval
            UNION ALL
            SELECT  'AT271729590' AS oldval, '600' AS newval
            UNION ALL
            SELECT  'AT275981020' AS oldval, '700' AS newval
            ) q
    ON      ae44.id = q.oldval
    SET     price = q.newval
    
    0 讨论(0)
  • 2020-11-27 19:45

    Remove the second "case" and it will work:

    UPDATE ae44
    SET Price = (CASE
    WHEN ID = 'AT259793380' THEN '500'
    WHEN ID = 'AT271729590' THEN '600'
    WHEN ID = 'AT275981020' THEN '700'
    END)
    
    0 讨论(0)
  • 2020-11-27 19:48

    You can try a simple query like:

    UPDATE `table`
    SET Price = ELT(field(ID,'AT259793380','AT271729590','AT275981020'), '500', '600', '700')
    WHERE ID IN ('AT259793380','AT271729590','AT275981020')
    
    0 讨论(0)
  • 2020-11-27 19:48
    update ae44
    set Price = 
    Case ID
    when 'AT259793380' then '500'
    when 'AT271729590' then '600'
    when 'AT275981020' then '700'
    end case
    
    0 讨论(0)
提交回复
热议问题