-1

我想将以下查询结果插入到新表中。请帮我这样做。这是查询

$result = mysql_query("SELECT item_name as 'Item Name', item_type as 'Item Type', 
                           (SUM(item_qty)-(
                                 SELECT SUM(srv_qty) FROM jos_sales 
                                 WHERE item_name=srv_name)
                           ) AS Balance,
                           (SUM(item_qty)-(
                                 SELECT SUM(srv_qty) FROM jos_sales 
                                 WHERE item_name=srv_name)
                           ) * item_price AS 'Total Cost' 
                       FROM jos_item c GROUP BY item_name");
4

3 回答 3

0

就像使用insert ...select语法一样简单:

 ("Insert into your_table ( columns )
   SELECT item_name as 'Item Name', item_type as 'Item Type', 
       (SUM(item_qty)-(
       ...
 ");
于 2012-10-20T10:07:41.670 回答
0

首先创建一个与查询的列结构匹配的新表,然后在查询前添加INSERT INTO newtable ...- 您可以将结果直接插入到表中,而无需通过 PHP 或其他检索接口运行它们。

于 2012-10-20T10:07:51.157 回答
0

利用INSERT... SELECT

INSERT INTO newTable(item_name, item_type, Balance, TotalCost)
SELECT item_name as 'Item Name', item_type as 'Item Type', 
                           (SUM(item_qty)-(
                                 SELECT SUM(srv_qty) FROM jos_sales 
                                 WHERE item_name=srv_name)
                           ) AS Balance,
                           (SUM(item_qty)-(
                                 SELECT SUM(srv_qty) FROM jos_sales 
                                 WHERE item_name=srv_name)
                           ) * item_price AS 'Total Cost' 
                       FROM jos_item c GROUP BY item_name
于 2012-10-20T10:09:26.473 回答