Suppose I have this code:
$ids = execute_query("SELECT id FROM table WHERE field = 'value' ORDER BY order_field");
$query = "UPDATE table SET increment = CASE";
for ($i = 0; $i <= sizeof($ids); $i++) {
$query .= " WHEN id = " . $ids[$i] . " THEN " . $i;
}
$query .= " END WHERE field = 'value'";
Then I execute that query.
Is there a way to combine the SELECT
and UPDATE
into one single query that achieves the exact same thing efficiently?