将多个查询结果插入到mysql表中

时间:2012-10-20 10:01:53

标签: mysql

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

$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");

3 个答案:

答案 0 :(得分:0)

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

 ("Insert into your_table ( columns )
   SELECT item_name as 'Item Name', item_type as 'Item Type', 
       (SUM(item_qty)-(
       ...
 ");

答案 1 :(得分:0)

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

答案 2 :(得分: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