I'm need to exclude some rows by id from SQL query results, but which way will be better to do this?
Using NOT IN by primary key id:
SELECT id, key1, key2 FROM my_table WHERE id NOT IN (#EXCLUDE_IDS)
Or in the post-processed loop after query in the script:
foreach ($rows as $i => $row) {
if (isset($exclude[$row['id']])) {
unset($rows[$i]);
}
}
# or
for ($i = count($rows) - 1; $i >= 0; $i--) {
if (isset($exclude[$rows[$i]['id']])) {
unset($rows[$i]);
}
}
not inlists can be slow. But in this case probably not slower than processing it in PHP