Pregunta

this is driving me nuts so any help would be greatly appreciated.

I'm trying to increment points totals in specific rows of a table by a number given by the user. I've got some PHP which generates this statement dynamically based on a form input:

UPDATE users SET points = CASE userID 
    WHEN 11 THEN (points + 1) 
    WHEN 16 THEN (points + 6) 
    WHEN 7 THEN (points + 7) 
    WHEN 10 THEN (points + 10) 
WHERE userID IN (11,16,7,10) 
END

I don't want to store the values in a PHP variable due to the issue of multiple accessors.

¿Fue útil?

Solución

Your syntax is wrong.

You need the "END" before the WHERE:

UPDATE users
    SET points =
        (CASE userID 
             WHEN 11 THEN (points + 1) 
             WHEN 16 THEN (points + 6) 
             WHEN 7 THEN (points + 7) 
             WHEN 10 THEN (points + 10) 
         end)
WHERE userID IN (11,16,7,10)

I reformatted the query. I make a point of putting the "END" for a "CASE" aligned in the same column to prevent this sort of problem.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top