我有一个select
我需要扫描表格来获取结果:
where (p.user in (select f from fl where user =? and block=0))
这个表是一个大表,有超过100万行。而且阅读起来需要一段时间。我需要一直阅读它,所以我想我可以阅读它一次,然后使用:
where (p.user in ($variable_with_all_results))
我尝试过:
$stmt = $mysqli->prepare("select f from f1 where user = '1' and block = 0");
$stmt->execute();
$stmt->bind_result($variable_with_all_results);
但是我不能在 select
上使用这个变量,mysql无法识别它。有什么想法吗?
最佳答案
你应该能够像这样做你想做的事:
$stmt = $mysqli->prepare("select GROUP_CONCAT(f) from f1 where user = '1' and block = 0");
$stmt->execute();
$stmt->bind_result($variable_with_all_results);
$stmt->fetch();
请注意,您需要在查询中的 f
周围添加 GROUP_CONCAT
聚合函数,这将为您提供一个类似 1,5,6
的列表>。此外,您还需要对语句执行fetch
,以将结果放入变量中。
然后您应该能够像这样进行新查询:
$sql = "SELECT ...
WHERE p.user in ($variable_with_all_results)";
关于php - 得到与mysql格式相同的结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50260734/