How does MySQL execute insert into ... select - mysql

As I know that many single insert statement is so slow. It can be better by handling like below.
insert into table1 values(c1,c2,c3) values(c4,c5,c6).
I'm interested in how insert into ... select ... execute in MySQL. Are there any optimizations in it or Innodb? At last, my English is not so good.Sorry - -||.

I think you are looking for this
INSERT INTO mytable (name, address, age)
VALUES('raj', 'jaipur', '15'),
('abc', 'india', '42'),
('xyz', 'jaipur ', '29'),
('taqi', 'Damsna', '32');

Related

Insert into.....values((select statement), value1, value2, value3) Select statement returns Multiple colums

Here's what I am trying to do..
INSERT IGNORE into table1(`name`,`phone`,`address`,`province`,`col1`, `col2`)
VALUES ((SELECT `name`,`phone`,`address`,`province` FROM table2
WHERE `country`= 'Canada'),'val1', 'val2');
Select statement basically returns multiple queries. How to handle scenario using a combo of values and select statement returning, multiple queries.
Thanks
Proper syntax:
INSERT IGNORE into table1(`name`,`phone`,`address`,`province`,`col1`, `col2`)
Select `name`,`phone`,`address`,`province`,'val1','val2'
from table2
where `country`= 'Canada'

how do you insert data into a table correctly using INSERT INTO?

I am new to SQL and relational databases, but am writing the code to add data into tables I have created. I am not sure if the code I have written is correct for inserting into a table. Can anyone tell me if this is correct please.
INSERT INTO Job (Job_ID, Job_name)
VALUES (‘1’, ‘Teacher’);
You code is right but if you use autoincrement column for job_id you should use
and use single quote s and not ‘
INSERT INTO Job ( Job_name)
VALUES ( 'Teacher');
otherwise the code you prodivided
INSERT INTO Job (Job_ID, Job_name)
VALUES ('1', 'Teacher );
You can also try like this :
insert into table_name set column_first = 'value_first' and column_second = 'value_second'
make correction:
INSERT INTO Job (Job_ID, Job_name)
VALUES ('1', 'Teacher');
You can insert value with this syntax
INSERT INTO table_name (column1,column2,column3,...)
VALUES (value1,value2,value3,...);
As per your code you can write
INSERT INTO Job (Job_ID, Job_name)
VALUES ('1', 'Teacher');
This my help:
w3schools
simple insertion
INSERT INTO table_name (column1,column2,column3,...)
VALUES (value1,value2,value3,...);

MYSQL bulkinsert: multi insert with value into a select

I have a question about the multi-ing (bulk) insert with mysql..
I know that:
INSERT INTO "my_table" ('col1','col2','col3') VALUES
(1,1,1),(2,2,2);
But I need to do something like:
INSERT INTO "my_table" ('col1','col2','col3') VALUES
((SELECT select1 as col1,select2 as col3 from "my_table2"),"textForAllCol2")
Where my select return a list of 2 column.
I'M trying to explain the best i can..
Thanks for your help!
JP
Here is referenced SQLFiddle for you
Modify your query as
INSERT INTO my_table SELECT col1, "textForAllCol2", col2 FROM my_table2;
You can use it like this
INSERT INTO "my_table" ('col1','col2','col3')
SELECT select1, "textForAllCol2", select2 from my_table2
try this
INSERT INTO my_table (`col1`,`col2`,`col3`)
SELECT select1, "textForAllCol2", select2 from my_table2
If your source and destination columns names are related (even partially) with each other, I prefer to use aliases for readability:
INSERT INTO my_table (`col1`,`col2`,`col3`)
SELECT `col1`, "textForAllCol2" as `col2`, `select3` as `col3` from my_table2

MYSQL syntax for inserting data to a table from another one which are not same form

It could be obvious but since I don't have any experience I cannot figure out how to do. Here is the question; say I have table2 with fields id,name, time, price and explanation.Also, there is an other table1 which has name, time and price. I want to insert data from table1 if price is higher than some threshold, while doing so as an explanation I want "The price is above threshold". Can anyone suggest me a way to do so? Thanks in advance.
Try this...
INSERT INTO `table2` (`name`, `time`, `price`, `explanation`)
SELECT `name`, `time`, `price`, 'The price is above threshold'
FROM `table1`
WHERE `price` > 100
INSERT table1 (name, time, price)
SELECT name, time, price
FROM table2
WHERE price > 120
You use an INSERT statement and list the fields you wish to insert into, followed by the SELECT statement you are going to use to populate the fields with. Here's a link: http://dev.mysql.com/doc/refman/5.5/en/insert.html.
Example:
INSERT INTO insert_tablename (field1, field2, field3)
SELECT field1, field2, field3
< The rest of your select statement >
NSERT [LOW_PRIORITY | HIGH_PRIORITY] [IGNORE]
[INTO] tbl_name [(col_name,...)]
SELECT ...
[ ON DUPLICATE KEY UPDATE col_name=expr, ... ]
With INSERT ... SELECT, you can quickly insert many rows into a table from one or many tables. For example:
INSERT INTO tbl_temp2 (fld_id)
SELECT tbl_temp1.fld_order_id
FROM tbl_temp1 WHERE tbl_temp1.fld_order_id > 100;
The following conditions hold for a INSERT ... SELECT statements:
http://dev.mysql.com/doc/refman/5.0/en/insert-select.html

SQL Insert Into a MAX value

In SQL how would I insert into a MAX count of another column
So I have this
INSERT INTO USER(id,name,employee_code,email) VALUES (3,john,(SELECT MAX(employee_code)+1 FROM USER),"john#example.com");
However this doesn't work with the syntax... basically it's like an autoincrement that I have to self build because the employee_code sometimes equals 0 for temporary employees so I need a command to take the max code in their and add one.
You probably forgot quotes around john:
INSERT INTO USER(id,name,employee_code,email)
VALUES (3, 'john', (SELECT MAX(employee_code)+1 FROM `USER`), "john#example.com");
Do away with the VALUES statement and use a select instead:
INSERT INTO USER(id,name,employee_code,email)
SELECT 3, 'john', MAX(employee_code)+1, "john#example.com"
FROM USER
You need to use an INSERT INTO... SELECT ...FROM query:
INSERT INTO USER(id,name,employee_code,email)
SELECT 3, 'john', MAX(employee_code)+1, 'john#example.com'
FROM `USER`;
INSERT INTO USER(id,name,employee_code,email)
SELECT
3,
'john',
MAX(employee_code)+1,
'john#example.com'
FROM USER