Muhammad
Muhammad

Reputation: 3250

how to use insert for multiple enteries in database

i have 8000 events in the database and i want to make this insert query for all, my current query is

where `event_id = 585,` 

it should be dynamic

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')
;

I know, I can achieve it using PHP select query and then insert, but can i do it using single insert query?

I want insert query automatic check in events table and then insert in event_site_text table?


updated:

i want when my insert query run it insert 8000 records for all the events in the database

Upvotes: 0

Views: 65

Answers (3)

avisheks
avisheks

Reputation: 1180

I think you are searching for something like:

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...)
;

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

Upvotes: 3

Alex
Alex

Reputation: 626

If you want to insert all the data from tb_a to tb_b use simple :

INSERT IGNORE INTO table_b SELECT * FROM table_a;

Upvotes: 0

KingOfTheNerds
KingOfTheNerds

Reputation: 653

Your example is almost there - you just need to add a comma between the records you want to insert. Unfortunately, this approach only allows you to insert up to 1000 records at a time, so you'll have to do a couple of inserts. Here's an example of a query that conveys the premise of what you're talking about (this would insert 4 records). I would suggest building the values as a string and then executing the entire SQL statement: 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');

Upvotes: 1

Related Questions