Home  >  Article  >  Database  >  A brief discussion on MySQL drifting (6)

A brief discussion on MySQL drifting (6)

黄舟
黄舟Original
2017-03-08 13:36:001019browse

Review the previous MySQL drifting story (1):

Add data:
Here we want to talk about, what should we do if we add multiple pieces of data?
Grammar: insert into table name values(…,…,…)
Case: insert into table04 values('wangwu',19,'beijing'),('gaojin',31,'nanjing');
Among them, values ​​are written to correspond to the fields when the table was created. They must be assigned once in the order when the table was created. If you forget the order of the fields when the table was created at this time, it does not matter. Use:
Syntax: describe The table name
can help us check the order when creating the table.
A brief discussion on MySQL drifting (6)

can give the column to be assigned a value, and then list the values. This is useful for those who wish to create records where only a few columns need to be initially set:

(1)insert test04(name) values('Jack');
(2)insert test01_01(name) values('Jack'),('baidu');

(3)insert test01_01 set name='Tencent';
//Using (3) this form of insert statement cannot insert multiple rows

insert…into…select.. statement:
Before we introduced that we can use the select statement to create a table, now we use the insert…into…select statement to provide us with convenience:

Use the insert into... select statement to meet the following conditions:

1: The query cannot contain an order by clause
2: The destination table of the insert statement cannot appear in the from clause of the select query part, because this is ANSI SQL prohibits selecting from the table you are inserting into. The problem is that select will likely find records that were previously inserted during the same run. When using sub-select clauses, the situation can be easily confused

Syntax example:
1)insert into table01_01 select * from table01_01;//Equivalent to copy, the table structure is completely consistent

2)insert into table01_01(name) select name from table04;//When copying – just take a certain value and add it
A brief discussion on MySQL drifting (6)

Review the previous MySQL drifting story (1):

Add data:
Here we want to talk about, what should we do if we add multiple pieces of data?
Grammar: insert into table name values(…,…,…)
Case: insert into table04 values('wangwu',19,'beijing'),('gaojin',31,'nanjing');
Among them, values ​​are written to correspond to the fields when the table was created. They must be assigned once in the order when the table was created. If you forget the order of the fields when the table was created at this time, it does not matter. Use:
Syntax: describe The table name
can help us check the order when creating the table.
A brief discussion on MySQL drifting (6)

can give the column to be assigned a value, and then list the values. This is useful for those who wish to create records where only a few columns need to be initially set:

(1)insert test04(name) values('Jack');
(2)insert test01_01(name) values('Jack'),('baidu');

(3)insert test01_01 set name='Tencent';
//Using (3) this form of insert statement cannot insert multiple rows

insert…into…select.. statement:
Before we introduced that we can use the select statement to create a table, now we use the insert…into…select statement to provide us with convenience:

Use the insert into... select statement to meet the following conditions:

1: The query cannot contain an order by clause
2: The destination table of the insert statement cannot appear in the from clause of the select query part, because this is ANSI SQL prohibits selecting from the table you are inserting into. The problem is that select will likely find records that were previously inserted during the same run. When using sub-select clauses, the situation can be easily confused

Syntax example:
1)insert into table01_01 select * from table01_01;//Equivalent to copy, the table structure is completely consistent

2)insert into table01_01(name) select name from table04;//When copying – only take a certain value and add
A brief discussion on MySQL drifting (6)


The above is the detailed content of A brief discussion on MySQL drifting (6). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn