将数据插入MySQL数据库,指定特定的表

时间:2015-01-09 10:09:46

标签: mysql sql-insert

我有一个查询

     SELECT ultrait_wpl_properties.id, location1_name, location3_name, location4_name, 
     field_312, field_42, post_code, lot_area, price, bedrooms, bathrooms, field_308, 
     googlemap_lt, googlemap_ln, street, street_no, ultrait_wpl_property_types.name, 
     ultrait_wpl_items.item_name 
     FROM ultrait_wpl_properties 
     JOIN ultrait_wpl_property_types 
     ON ultrait_wpl_properties.property_type = ultrait_wpl_property_types.id 
     JOIN ultrait_wpl_items ON ultrait_wpl_properties.id = ultrait_wpl_items.id 
     ORDER BY ultrait_wpl_properties.id

正如您所看到的,我正在使用JOINS。在插入新记录时,有没有办法创建一个INSERT语句,它将包含这些?

我的意思是,如果我拥有上述查询中指定的所有字段的数据,我可以写一个INSERT,将数据插入到正确的表中吗?我目前正在研究此事,但如果有人能提供任何见解,我们将不胜感激。

1 个答案:

答案 0 :(得分:0)

没有

或稍长的答案:不,SQL标准不提供此选项。

有一种解决方法:您可以创建一个STORED PROCEDURE来接受查询中的输入并将其拆分为多个INSERT语句。但是:此解决方案不具备标准SQL语句的任何灵活性。

如果你有三个表通过键连接也不是一件简单的事情,你可能还有2个更新和一个插入而不是三个插入。