前言
insert語句是標準sql中的語法,是插入數(shù)據(jù)的意思。在實際應用中,它也演變了很多種用法來實現(xiàn)特殊的功能,下面介紹在mysql數(shù)據(jù)庫中insert語句的五種用法。
一、values參數(shù)后單行插入
語法:
insert into tableName (colunm1,colunm2,...) value(value1,value2,...);
如果插入多條數(shù)據(jù),需要寫多條sql。
insert into a(id,name,type) values (1,'A1','T1');
insert into a(id,name,type) values (2,'A2','T2');
二、values參數(shù)后多行插入
語法:
insert into tableName(colunm1,colunm2,..) values(value1,value2...),(value1,value2...);
多條數(shù)據(jù)1條sql即可,相較于方法1效率更高。
insert into a(id,name,type) values (1,'A1','T1'),(2,'A2','T2');
三、搭配select插入數(shù)據(jù)
語法:
insert into tableName(colunm1,colunm2,..) select colunm1,colunm2,..;
多條數(shù)據(jù)使用union all關聯(lián)即可。
insert into a(id,name,type)
select 1,'A1','T1'
union all
select 2,'A2','T2';
四、復制舊表的信息到新表
語法:
insert into tableName(colunm1,colunm2,..) select colunm1,colunm2,.. from tableName1;
假設兩個表的表結(jié)構一樣則語句如下,否則請指定字段名稱。
insert into a select * from b where id=1;
五、搭配set插入數(shù)據(jù)
語法:
insert into tableName set colunm1=value1,colunm2=value2....;
使用set是拓展寫法,可以精準的對列賦值,防止賦值時由于順序混亂導致的數(shù)據(jù)錯誤,同時這種寫法插入數(shù)據(jù)的速度更快,但不適合批量循環(huán)插入。文章來源:http://www.zghlxwxcb.cn/news/detail-424474.html
insert into a set id=1,name='A1',type='T1';
總結(jié)
word文檔下載地址:mysql中insert語句的五種用法文章來源地址http://www.zghlxwxcb.cn/news/detail-424474.html
到了這里,關于mysql中insert語句的五種用法的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!