mysql補(bǔ)齊缺省數(shù)據(jù)?
網(wǎng)友解答: 你給的信息太少,我給你舉例吧,假設(shè)下表B,是這樣的(--只是為了格式):id class_id class info1 ----- 1 --
你給的信息太少,我給你舉例吧,假設(shè)下表B,是這樣的(--只是為了格式):
id class_id class info
1 ----- 1 ------ 明星 。。。。。
2 --------------- 軍事 。。。。。
3 ------ 3------- ---------- 。。。。。
4 ---------------- ---------- 高考分?jǐn)?shù)
類(lèi)別表C
class_id class
1 ---- 明星
2 ----- 軍事
3 -----體育
4 -----高考
比如上面這樣的一個(gè)情況,你需要補(bǔ)齊類(lèi)別信息,class_id或者class信息。
一、首先,使用select統(tǒng)計(jì)缺失情況。
統(tǒng)計(jì)class_id缺失的有多少:select count(*) from b where class_id is null or class_id = ' ' and class is not null;
統(tǒng)計(jì)class缺失的有多少:select count(*) from b where class is null or class_id = ' ' and class_id is not null;
在正式更新之前,建議,先備份一次,或者創(chuàng)建一張復(fù)制表。
create table b_test select * from b; 數(shù)據(jù)太大,就換一種方式:
create table b_test select * from b where 1=2; 這樣就只創(chuàng)建一張表結(jié)構(gòu),沒(méi)有數(shù)據(jù),
裝載數(shù)據(jù):
insert into b_test select * from b where id <20000;
依次類(lèi)推,直到裝載完畢。千萬(wàn)不要直接在原表上直接更新。
如果統(tǒng)計(jì)的數(shù)量較多,例如超過(guò)10w,請(qǐng)分批執(zhí)行。
補(bǔ)齊class:update b_test,c set b_test.class=c.class where b_test.class_id=c.class_id and b_test.id in (select id from b_test where class is null or class_id = ' ' and class_id is not null limit 0,20000);
每次只會(huì)2w行的更新,多次執(zhí)行上面SQL語(yǔ)句,對(duì)應(yīng)修改 b_test.id in (select id from b_test where class is null or class_id = ' ' and class_id is not null limit 20000,40000);
依次類(lèi)推。
補(bǔ)齊class_id:其實(shí)也一樣:
update b_test,c set b_test.class_id=c.class_id where b_test.class=c.class and b_test.id in (select id from b_test where class is null or class_id = ' ' and slass_id is not null limit 0,20000 ;
使用統(tǒng)計(jì)的SQL,確認(rèn)一次,是否還有沒(méi)有修改到的。
最后剩下,class和class_id都沒(méi)有的,
你可能只有手動(dòng)處理了?;蛘?/p>
update b_test set b_test.id=4,b_test.class='高考' where info like '%高考%';
希望能幫助你解決問(wèn)題。有任何問(wèn)題,歡迎私信我。
網(wǎng)友解答:直接使用語(yǔ)句設(shè)置字段值為0就可以了 update 表名 set field = 0