Jump to content

Recommended Posts

Having a bit of a problem forming an update query using a CASE statement in it. Can successfully do when using a SELECT query but my syntax is wrong with all the attempts I've made to use UPDATE. Here is my working select query

 

$q = "select machine_seq,
CASE machine_seq
WHEN $prev_seq then $seq
WHEN $seq then $prev_seq
END AS new_machine_seq
from workout_routines
where user_id = '$userid' and routine_id = '$routine' and
machine_seq in ($prev_seq,$seq)";

 

Can someone alter this to an UPDATE query for me? I'm trying to swap two values of the column named 'machine-seq'.

Link to comment
https://forums.phpfreaks.com/topic/297494-update-query-with-case/
Share on other sites

You assign the column value as the case

UPDATE workout_routines
SET machine_seq =
	CASE machine_seq
	WHEN $prev_seq THEN $seq
	WHEN $seq THEN $prev_seq
	END
WHERE user_id = '$userid' AND routine_id = '$routine' AND
machine_seq IN ($prev_seq,$seq)
Edited by Ch0cu3r
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.