I've searched through Stackoverflow and nothing answers my question properly.
My question is how do you turn multiple INSERT queries into 1 single insert query.
More specific; https://gist.github.com/gregariousjb/e73fdf1489acbbb63651 this one. That's the query I need to understand how to make into a single one.
Sample;
INSERT INTO `creature` (`guid`, `id`, ...)
VALUES (1, 2843, ...);
INSERT INTO `creature` (`guid`, `id`, ...)
VALUES (2, 7853, ...);
There's 1000 of these, that needs to be turned into a single one. I sincerely appreciate any help I can get on this.
You can use a PRIMARY KEY or a UNIQUE Index on a table with the appropriate fields to stop duplicate records. Let us take an example – The following table contains no such index or primary key, so it would allow duplicate records for first_name and last_name.
If you do not include DISTINCT in a SELECT clause, you might find duplicate rows in your result, because SQL returns the JOB column's value for each row that satisfies the search condition.
If you are using Sql Server try the following
Insert into table (columns..)
Values(values1,value2,...),
(values1,value2,...),
(values1,value2,...),
(values1,value2,...)
In Mysql, do this (most popular databases have a similar syntax):
INSERT INTO mytable (col1, col2, col3, ...) VALUES
(1, 2843, 0, ...),
(2, 7853, 0, ...);
In most databases, you can do this:
INSERT INTO mytable (col1, col2, col3, ...)
SELECT 1, 2843, 0, ...
UNION ALL
SELECT 2, 7853, 0, ...;
In backward, behind-the-times databases, like Oracle, you must code this second option using the artificial single-rowed table DUAL:
INSERT INTO mytable (col1, col2, col3, ...)
SELECT 1, 2843, 0, ...
FRIM DUAL
UNION ALL
SELECT 2, 7853, 0, ...
FROM DUAL;
The closest is the shorthand version that requires the field list only once:
INSERT INTO `creature` ( <field list> ) VALUES
( <value list> ),
( <value list> ),
( <value list> )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With