如果我有一个select语句,我怎样才能轻松地将其分解为mysql中的insert语句?

时间:2012-01-17 21:07:26

标签: mysql

示例:

select * 
from 
  component 
inner join 
  component_supplier on component.id = component_supplier.component_id
inner join 
  supplier on supplier.id = component_supplier.supplier_id
where 
  component_supplier.supplier_part_no = ".048_CR_SHT_A1008_CS_TP_ B_4ftx8ft"

我想获取这些数据并将其删除并将其填入另一个表中,如下所示:

  INSERT into sugarcrm2.jce_components jce_c(   
    id,
    name,
    part_no_id,
    part_no,
    cost,
    lead,
    qty,   
    units,   
    class,   
    category,   
    notes ) 
  VALUES(   
    a bunch of different values that I get from that first select. 
  )

3 个答案:

答案 0 :(得分:2)

INSERT可采用以下形式:

INSERT INTO TABLE2(A,B,C) 
SELECT A, B, C 
FROM TABLE1 
INNER JOIN...

答案 1 :(得分:1)

通常情况下,“插入table_x(字段列表)中选择(您的选择语句)将是您想要的。您不希望为此选择*,因为字段的顺序需要匹配。” / p>

例如

INSERT INTO tbl_temp2 (fld_id)
SELECT tbl_temp1.fld_order_id
FROM tbl_temp1 WHERE tbl_temp1.fld_order_id > 100;

答案 2 :(得分:0)

INSERT into sugarcrm2.jce_components jce_c(id,   name,
    part_no_id,   part_nocost,   lead,   qty,   units,   
    class,   category,   notes )
select component.id,   component.name,   component.part_no_id,   
    part_nocost,   lead,   qty,   units,   class,   category,   notes  
from    component  
    inner join    component_supplier on component.id = component_supplier.component_id 
    inner join    supplier on supplier.id = component_supplier.supplier_id 
where    component_supplier.supplier_part_no = ".048_CR_SHT_A1008_CS_TP_ B_4ftx8ft" 

我不知道哪个值在哪个表中,但是你必须在select之后执行.column。