Jump to content

[SOLVED] MYSQL IF Statement in Query


Silverado_NL

Recommended Posts

hi everyone.

i am currently making a website with a certain switch that goes 2 ways, On and Off , handled by 1 and 0

now i have this query to handle and switch this value

$query ="UPDATE table SET switch = IF(switch = '0', '1', IF(switch = '1', '0', switch)) WHERE record_id = $record_id";

but a 3rd value is needed now.

 

but how do i check this in the query????

 

INSTEAD OF: logical query being, :::::    if switch == 0 then make 1 , if switch == 1 then make 0

 

it now needs to be like this, ::::::::::    if switch == 0 then make 1 , if switch == 1 then make 2 , if switch == 2 then make 0

does anybody have any idea????

i cant find any info on If statement in query's on the internet.

so i thought ill ask here:):):)

 

 

thanks in advance...

Silverado

 

Link to comment
https://forums.phpfreaks.com/topic/44620-solved-mysql-if-statement-in-query/
Share on other sites

already solved:)

i wrote this query.

db_query("UPDATE table SET switch = IF(switch = '0', '1', IF(switch = '0', '1', IF(switch = '1', '2', IF(switch = '2', '0', switch)))) WHERE record_id = $record_id");

 

i had the table record settings on Enum '0','1' instead of Enum '0','1','2' !!!!!!!!!!!!!!!

that's why it didnt work at first, and i thought i wrote the query on a wrong way.

thanks anyways:)

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.