如何在数据库中使用insert作为多个条目

时间:2014-12-24 07:08:30

标签: php mysql

我在数据库中有8000个事件,我想对所有人进行此插入查询,我当前的查询是

where `event_id = 585,` 

它应该是动态的

INSERT INTO 
event_site_text 
(event_id, constant_value,default_value, value, section,section_order,constant_order)
 VALUES
(585, 'REGISTER_TICKET_END', '11.49- Register Ticket End','Registration tickets are full, please contact event organizer.','Billing', '11', '49')
;

我知道,我可以使用PHP select查询然后insert来实现它,但是我可以使用单个插入查询来实现吗?

我想在events表中插入查询自动检查,然后插入event_site_text表中?


更新:

我希望当我的插入查询运行时,它会为数据库中的所有事件插入8000条记录

3 个答案:

答案 0 :(得分:3)

我认为你正在寻找类似的东西:

INSERT INTO
event_site_text 
(event_id, constant_value,default_value, value, section,section_order,constant_order)
SELECT
event_id, constant_value,default_value, value, section,section_order,constant_order
FROM
events
WHERE event_id in(585,other_id, another_id...)
;

参考:http://dev.mysql.com/doc/refman/5.6/en/insert-select.html

答案 1 :(得分:1)

您的示例几乎就在那里 - 您只需要在要插入的记录之间添加逗号。不幸的是,这种方法只允许您一次最多插入1000条记录,因此您必须进行几次插入。这是一个查询的例子,它传达了你所谈论的前提(这将插入4条记录)。我建议将值构建为字符串,然后执行整个SQL语句:INSERT INTO example VALUES (100, 'Name 1', 'Value 1', 'Other 1'), (101, 'Name 2', 'Value 2', 'Other 2'), (102, 'Name 3', 'Value 3', 'Other 3'), (103, 'Name 4', 'Value 4', 'Other 4');

答案 2 :(得分:0)

如果您要将tb_a中的所有数据插入tb_b,请使用简单:

INSERT IGNORE INTO table_b SELECT * FROM table_a;

相关问题