I'm not an SQL expert and a quick google search hasn't gotten me much (though I don't think I'm using the best search phrases). Hope you guys can give me some guidance.
I have a certain column in a table that stores a number. I want this number to be set to 2 ONLY if it previously equaled 1. I was wondering if it was possible to avoid this:
$val = mysql_fetch_row(mysql_query("SELECT columnA FROM tableName WHERE something = '$someValue'));
if($val[0] == 1) { .....UPDATE tableName SET columnA = 2....... }
Is it possible to directly query the database? With something like this: SET columnA = 2 if columnA = 1
解决方案
Actually the query you're looking for should be:
update tableName set columnA = 2
where columnA = 1
The "if" you're mentioning is actually a condition. And these conditions are set in the where clause of the update statement.
That's it :)