mysql如何添加數(shù)據(jù)
10年積累的網(wǎng)站制作、成都做網(wǎng)站經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶對(duì)網(wǎng)站的新想法和需求。提供各種問(wèn)題對(duì)應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先網(wǎng)站制作后付款的網(wǎng)站建設(shè)流程,更有下花園免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
舉例如下:
//建立一個(gè)表
create table tab_nam(
age int,
name char(10)
)
//往里插入數(shù)據(jù)
insert into tab_nam(age,name) values(11,'aaa')
insert into tab_nam(age,name) values(22,'bbb')
......
以mysql數(shù)據(jù)庫(kù)為例分情況一一說(shuō)明:
兩張表:insertTest和insertTest2,前者中有測(cè)試數(shù)據(jù)
create table insertTest(id int(4),name varchar(12));
insert into insertTest values(100,'liudehua');
insert into insertTest values(101,'zhourunfa');
insert into insertTest values(102,'zhouhuajian');
1.如果2張表的字段一致,并且希望插入全部數(shù)據(jù),可以用這種方法:
INSERT INTO 目標(biāo)表 SELECT * FROM 來(lái)源表;
insert into insertTest select * from insertTest2;
2.如果只希望導(dǎo)入指定字段,可以用這種方法:
INSERT INTO 目標(biāo)表 (字段1, 字段2, ...) SELECT 字段1, 字段2, ... FROM 來(lái)源表;
注意字段的順序必須一致。
insert into insertTest2(id) select id from insertTest2;
3.如果您需要只導(dǎo)入目標(biāo)表中不存在的記錄,可以使用這種方法:
INSERT INTO 目標(biāo)表
(字段1, 字段2, ...)
SELECT 字段1, 字段2, ...
FROM 來(lái)源表
WHERE not exists (select * from 目標(biāo)表
where 目標(biāo)表.比較字段 = 來(lái)源表.比較字段);
1.插入多條記錄:
insert into insertTest2
(id,name)
select id,name
from insertTest
where not exists (select * from insertTest2
where insertTest2.id=insertTest.id);
2.插入一條記錄:
insert into insertTest
(id, name)
SELECT 100, 'liudehua'
FROM dual
WHERE not exists (select * from insertTest
where insertTest.id = 100);
使用 dual 作表名,select 語(yǔ)句后面直接跟上要插入的字段的值。
4.將查詢出來(lái)的數(shù)據(jù)并同其他變量一起插入新的數(shù)據(jù)表中
insert into t_supp_PurchPlan_s(PurPlanCode,itemcode,Speccode) select 'hello'as PurPlanCode,itemcode,speccode from b_item where id=8
直接將變量放到相應(yīng)的位置即可(如上將固定的變量或動(dòng)態(tài)變量放入即可)
方法一,從已有大數(shù)據(jù)表中檢索大量數(shù)據(jù)插入到目標(biāo)表里;
方法二,編寫(xiě)存儲(chǔ)過(guò)程,利用循環(huán)向數(shù)據(jù)表中插入大量的固定或有規(guī)律變化或隨機(jī)變化的虛擬數(shù)據(jù);
方法三,通過(guò)應(yīng)用程序端編程向目標(biāo)表插入大量的數(shù)據(jù),手法與方法二類(lèi)似。
用shell腳本通過(guò)while循環(huán)批量生成mysql測(cè)試數(shù)據(jù)的方法。
1、很多時(shí)候需要在mysql表中插入大量測(cè)試數(shù)據(jù),下面分享一個(gè)用shell腳本通過(guò)while循環(huán)批量生成mysql測(cè)試數(shù)據(jù)的方法,你只需要根據(jù)你自己的表結(jié)構(gòu)來(lái)生成sql語(yǔ)句即可。
復(fù)制代碼代碼如下:
#!/bin/bash
i=1;
MAX_INSERT_ROW_COUNT=$1;
while [ $i -le $MAX_INSERT_ROW_COUNT ]
do
mysql -uroot -proot afs -e "insert into afs_test (name,age,createTime) values ('HELLO$i',$i % 99,NOW());"
d=$(date +%M-%d\ %H\:%m\:%S)
echo "INSERT HELLO $i @@ $d"
i=$(($i+1))
sleep 0.05
done
exit 0
2、假定上面的shell腳本保存為create-data.sh,可以通過(guò)下面的命令來(lái)生成數(shù)據(jù):
復(fù)制代碼代碼如下:sh create-data.sh 10000。(參數(shù)10000是要生成的數(shù)據(jù)條數(shù)。)
使用insert語(yǔ)句插入數(shù)據(jù):
//注意數(shù)據(jù)1,數(shù)據(jù)2....要對(duì)應(yīng)表的字段
insert?into?表的名稱?values?(數(shù)據(jù)1,?數(shù)據(jù)2,....)
或者指定列名插入數(shù)據(jù):
//列1對(duì)應(yīng)數(shù)據(jù)1
insert?into?表的名稱?(列1,?列2,...)?values?(數(shù)據(jù)1,?數(shù)據(jù)2,....)
文章標(biāo)題:mysql數(shù)據(jù)怎么插,怎么在mysql添加數(shù)據(jù)
文章轉(zhuǎn)載:http://m.kartarina.com/article24/dseicje.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、外貿(mào)建站、面包屑導(dǎo)航、ChatGPT、域名注冊(cè)、關(guān)鍵詞優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)