php - 得到与mysql格式相同的结果?

标签 php mysql

我有一个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/

相关文章:

php - 最佳邀请数据模型?

php - 无法更新名称为 'routing' 的列

mysql根据字段值自定义用户权限

mysql - SQL : Ideal engine type (MyISAM vs InnoDB) and data type for unique text column

mysql - 从 mysql 获取最新事件

php - 在查询中包含变量($ lecno)时,SQL查询出错

javascript - 套接字代码在Codeigniter Rest API内部不起作用

php - 让 PHP Eclipse 对不同的文件类型运行错误检查

php - 自动完成电影名称

java - 带有用于搜索 Web 应用程序的可选字段的 SQL 查询