I'd like to retrieve a number of rows from a MySQL table, and remember which ones I've retrieved later. (I'm sending these to an API and I need to track whether they were accepted or not).
The way I'd like to achieve this is to use an UPDATE
query in order to create a batch_id
for 100 records or so. I'd also like to get the batch_id
returned.
Here's what I've come up with so far (complete PHP function):
function get_batch() {
global $DB;
$q = $DB->prepare("UPDATE my_table
SET batch_id = LAST_INSERT_ID(MIN(id))
WHERE batch_id = ''
LIMIT 100");
$q->execute();
return $DB->lastInsertId();
}
This is an invalid use of a group function (because I can't use MIN on all rows at the same time as updating individual rows, presumably).
I could use a subquery to find the lowest ID, but I wondered if there was a better way of doing this?
I'm also not sure if my use of LAST_INSERT_ID
is correct in this context (setting it to retrieve it later)?