bonaca bonaca - 4 months ago 7
SQL Question

execute SET only for specific values?

$x = 5;
$stmt = $db->prepare('UPDATE ' . $table. ' SET sort = sort + 1') ;
$stmt->execute();


How can I execute the query only for those values in
sort
column which are equal or higher than
$x
?

Answer

Simply add it to as where statement:

$stmt = $db->prepare('UPDATE ' . $table. ' SET sort = sort + 1 where sort >= ?') ;
$stmt->bind(1, $x)