插入到临时表的 sql 查询的结果


Result of sql query inserted to temporary table

我在php中有这个sql查询:-

$sql = "SELECT customer.id,order.code,
FROM customer
INNER JOIN order 
ON customer.name = order.name";

打算将其插入我创建的临时表中,该怎么做?

因为临时表架构不可用。我假设,

表名temporary_table_name ; 具有

id(自动增量、主键)、customer_idorder_code

根据需要进行更改。

插入...选择,您可以快速将许多行插入表格 从一个或多个表。

使用此查询(从中更改temporary table namecolumn name后)。

$sql = "INSERT INTO temporary_table_name (customer_id, order_code) 
        SELECT customer.id,order.code,
        FROM customer
        INNER JOIN order 
        ON customer.name = order.name";

有关详细信息,请单击

  1. 在一个查询中插入和选择 - Mysql 文档
  2. 如何在MySQL"insert"语句中使用"选择" - 堆栈溢出

我假设您需要在临时表中存储两列 avlues customer.idorder.code

首先创建临时表假设customer_temp

    CREATE TEMPORARY TABLE customer_temp(
    customer_id INTEGER,
    order_code INTEGER 
);

然后将值插入临时表中,如下所示:

INSERT INTO customer_temp 
    SELECT customer.id,order.code,
FROM customer
INNER JOIN order 
ON customer.name = order.name;

现在,从临时表中获取记录,如下所示: Select * from customer_temp .

注意:临时表仅适用于会话。