gpt4 book ai didi

php - Laravel 3 中的复杂 sql 语句

转载 作者:行者123 更新时间:2023-11-29 08:10:26 35 4
gpt4 key购买 nike

这是我现在的代码

    $piv0 = "SET @pivot_query = NULL;";
$piv1 = "SELECT GROUP_CONCAT(DISTINCT CONCAT('MAX(IF(custom_field = \"\"',custom_field,\"\"', cf_values, NULL)) AS ',custom_field)) INTO @pivot_query FROM custom_fields_values; ";

$piv2 = "SET @pivot = CONCAT('SELECT subscriber_id, ', @pivot_query, ' FROM custom_fields_values GROUP BY subscriber_id');";

$piv3 = "SET @sql = CONCAT('SELECT * FROM user_data_3 LEFT JOIN (',@pivot,') piv ON user_data_5.aw_id = piv.subscriber_id');";
$piv4 = "PREPARE stmt FROM @sql; ";
$piv5 = "EXECUTE stmt;";
$piv6 = "DEALLOCATE PREPARE stmt;";

$db = DB::connection()->pdo;
$db->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);

$query = $db->prepare($piv0);
$query->execute();

$query = $db->prepare($piv1);
$query->execute();

$query = $db->prepare($piv2);
$query->execute();

$query = $db->prepare($piv3);
$query->execute();

$query = $db->prepare($piv4);
$query->execute();

$query = $db->prepare($piv5);
$query->execute();

$query = $db->prepare($piv6);
$query->execute();

$result = $query->fetchAll();

但是,我收到错误:

General error: 2014 Cannot execute queries while other unbuffered queries are active.  Consider using PDOStatement::fetchAll().  Alternatively, if your code is only ever going to run against mysql, you may enable query buffering by setting the PDO::MYSQL_ATTR_USE_BUFFERED_QUERY attribute.

我查看了其他一些 SO 问题,有人提到 $query->fetchAll() 和/或 $query->closeCursor() 应该有所帮助,但是他们在这里没有帮助我。

我在 $query = $db->prepare($piv4);

上收到错误

我还应该尝试什么?

最佳答案

您正在执行部分或一个查询,而您应该通过添加它们。。尝试像这样执行它们。

$piv = "SET @pivot_query = NULL;";
$piv .= "SELECT GROUP_CONCAT(DISTINCT CONCAT('MAX(IF(custom_field = \"\"',custom_field,\"\"', cf_values, NULL)) AS ',custom_field)) INTO @pivot_query FROM custom_fields_values; ";

$piv .= "SET @pivot = CONCAT('SELECT subscriber_id, ', @pivot_query, ' FROM custom_fields_values GROUP BY subscriber_id');";

$piv .= "SET @sql = CONCAT('SELECT * FROM user_data_3 LEFT JOIN (',@pivot,') piv ON user_data_5.aw_id = piv.subscriber_id');";
$piv .= "PREPARE stmt FROM @sql; ";
$piv .= "EXECUTE stmt;";
$piv .= "DEALLOCATE PREPARE stmt;";
$db = DB::connection()->pdo;

$query = $db->prepare($piv);
$query->execute();
$result = $query->fetchAll();

编辑:

 $piv = "SET @pivot_query = NULL;
SELECT GROUP_CONCAT(DISTINCT CONCAT('MAX(IF(custom_field = \"\"',custom_field,\"\"', cf_values, NULL)) AS ',custom_field)) INTO @pivot_query FROM custom_fields_values;
SET @pivot = CONCAT('SELECT subscriber_id, ', @pivot_query, ' FROM custom_fields_values GROUP BY subscriber_id');
SET @sql = CONCAT('SELECT * FROM user_data_3 LEFT JOIN (',@pivot,') piv ON user_data_5.aw_id = piv.subscriber_id');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;";

关于php - Laravel 3 中的复杂 sql 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21728225/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com