简体   繁体   English

在 PHP 中运行两个 SQL 查询,其中第一条语句创建一个临时表供第二条语句使用

[英]Running two SQL queries in PHP where the first statement creates a temporary table to be used by the second one

I want to run this long SQL query in PHP.我想在 PHP 中运行这个长 SQL 查询。

CREATE TEMPORARY TABLE IF NOT EXISTS info AS (
  SELECT warehouse.merchandise_id, SUM(warehouse.prod_quantity) AS qty 
  FROM warehouse 
  WHERE warehouse.merchandise_id IN (SELECT merchandise.id FROM merchandise) 
  GROUP BY warehouse.merchandise_id
); 
SELECT LPAD(`id`,8,'0'), prod_title, prod_lcode, prod_price, qty 
FROM `merchandise` INNER JOIN info 
  ON merchandise.id = merchandise_count.merchandise_id;

Here's a quick explanation of what it does: First it creates a temporary table to store some selected data, then it uses the temporary table to INNER JOIN it with data in a permanent table.下面是它所做的快速解释:首先它创建一个临时表来存储一些选定的数据,然后它使用临时表将它与永久表中的数据进行 INNER JOIN。

I have already tried '$statement1;$statement2;'我已经尝试过'$statement1;$statement2;' in PHP, but it gives syntax and access violation error but the given query works flawlessly in phpmyadmin.在 PHP 中,但它给出了语法和访问冲突错误,但给定的查询在 phpmyadmin 中完美运行。

I checked other similar posts like this and they suggest to use '$statement1;$statement2;'我检查了其他类似帖子,他们建议使用'$statement1;$statement2;' but it doesn't work for me.但这对我不起作用。 My server is running PHP 7. I'm using PHP PDO to connect to my database.我的服务器正在运行 PHP 7。我正在使用 PHP PDO 连接到我的数据库。 Any help is appreciated.任何帮助表示赞赏。

I ran the following and it did work.我运行了以下命令,它确实有效。

$stmt = $pdo->query("
CREATE TEMPORARY TABLE IF NOT EXISTS info AS (
  SELECT warehouse.merchandise_id, SUM(warehouse.prod_quantity) AS qty
  FROM warehouse
  WHERE warehouse.merchandise_id IN (SELECT merchandise.id FROM merchandise)
  GROUP BY warehouse.merchandise_id
);
SELECT LPAD(`id`,8,'0'), prod_title, prod_lcode, prod_price, qty
FROM `merchandise` INNER JOIN info
  ON merchandise.id = info.merchandise_id;
");

// skip to next rowset, because it's a fatal error to fetch from a statement that has no result
$stmt->nextRowset();

do {
    $rowset = $stmt->fetchAll(PDO::FETCH_NUM);
    if ($rowset) {
        print_r($rowset);
    }
} while ($stmt->nextRowset());

Notice I had to fix merchandise_count.merchandise_id to info.merchandise_id in your query, because you have no table reference to merchandise_count .请注意,我必须在您的查询中将merchandise_count.merchandise_id info.merchandise_id修复为 info.merchandise_id,因为您没有对merchandise_count计数的表引用。

However, I would recommend you do not bother with multi-query.但是,我建议您不要为多查询而烦恼。 There's no benefit from concatenating multiple SQL statements in a single call.在单个调用中连接多个 SQL 语句没有任何好处。 It's also not supported to use prepared statements when using multi-query, or to define stored routines like procedures, functions, or triggers.也不支持在使用多查询时使用准备好的语句,或者定义存储例程,如过程、函数或触发器。

Instead, execute the statements one at a time.相反,一次执行一个语句。 Use exec() if the statement has no result set and doesn't need to be prepared statements.如果语句没有结果集并且不需要准备语句,请使用exec()

$pdo->exec("
CREATE TEMPORARY TABLE IF NOT EXISTS info AS (
  SELECT warehouse.merchandise_id, SUM(warehouse.prod_quantity) AS qty
  FROM warehouse
  WHERE warehouse.merchandise_id IN (SELECT merchandise.id FROM merchandise)
  GROUP BY warehouse.merchandise_id
)");

$stmt = $pdo->query("
SELECT LPAD(`id`,8,'0'), prod_title, prod_lcode, prod_price, qty
FROM `merchandise` INNER JOIN info
  ON merchandise.id = info.merchandise_id
");

$rowset = $stmt->fetchAll(PDO::FETCH_NUM);
if ($rowset) {
  print_r($rowset);
}

As long as you use the same $pdo connection, you can reference temporary tables in subsequent queries.只要使用相同的$pdo连接,就可以在后续查询中引用临时表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM