选择一组具有已知随机ID的记录

时间:2015-07-06 21:16:27

标签: php mysql pdo

从数组中选择带有随机ID的相当大量(20-50)行的最佳方法是什么?

例如,这样的代码可以工作(我猜)

$array = [1, 53, 11, 334, 5, 96, ...]; // about 50 elements but could be more;

$sql = 'SELECT * FROM table WHERE id IN (' . substr(str_repeat('?,', count($array)), -1) . ')';

$st = $pdo->prepare($sql);
$st->execute($array);

$st->fetchAll(); // results

我记得我过去读过的答案建议避免IN,因为它很慢。

执行此操作的最佳技术是什么?

0 个答案:

没有答案