mysql语法之update
Update 語(yǔ)句:
1、作用:Update 語(yǔ)句用于修改表中的數(shù)據(jù)。
語(yǔ)法:UPDATE 表名稱 SET 列名稱 = 新值 WHERE 列名稱 = 某值
1.建表語(yǔ)句:
create table table1(
idd varchar(10),
val varchar(20)
);
create table table2(
idd varchar(10),
val varchar(20)
);
2.插入數(shù)據(jù):
insert into table1 values (‘01’,‘1111’);
insert into table1 values (‘02’,‘222’);
insert into table1 values (‘02’,‘2222’);
insert into table1 values (‘03’,‘3333’);
insert into table1 values (‘04’,‘4444’);
insert into table1 values (‘06’,‘6666’);
insert into table2 values (‘01’,‘a(chǎn)aaa’);
insert into table2 values (‘02’,‘bbbb’);
insert into table2 values (‘03’,‘cccc’);
insert into table2 values (‘04’,‘dddd’);
insert into table2 values (‘05’,‘eee’);
(1)列一:更新某一行中的一個(gè)列
**update table1 set val='022222' where id='02'**(2)更新某一行中的若干列
alter table table1 add column cs varchar(45) ;
alter table table2 add column cs varchar(45) ;
update table1 set val=‘022222’,cs=‘222’ where idd='02’
(3) mysql數(shù)據(jù)庫(kù)update更新表中某個(gè)字段的值為另一張表的某個(gè)字段值
update table1 a left join table2 b on a.idd= b.idd set a.val = b.val where a.idd=b.idd;
(4)mysql查詢出一張表的數(shù)據(jù)值去更新另一張表的數(shù)據(jù)值
update table1 set val=(SELECT val FROM table2 where idd=‘01’) where idd=‘03’
(5)對(duì)某些字段變量+1,常見(jiàn)的如:點(diǎn)擊率、下載次數(shù)等這種直接將字段+1然后賦值給自身
update table1 set val=val+1 where idd=‘06’
(6)將同一個(gè)表中的一些記錄更新到另外一些記錄中
create table price
(
id int,
month int,
r_id int,
price int
);
insert into price(id,month,r_id,price) values(1,1,1,2);
insert into price(id,month,r_id,price) values(2,1,1,4);
insert into price(id,month,r_id,price) values(3,2,1,5);
insert into price(id,month,r_id,price) values(4,2,2,5);
表:price
ID month R_ID Price
1 1 1 2
2 1 2 4
3 2 1 5
4 2 2 5
要求:如何將表中2月份的產(chǎn)品price更新到1月份中
處理方法:要找到2月份中和1月份中ID相同的E_ID并更新price到1月份中
方法一:update price as a,price as b set a.price=b.price where a.r_ID=b.r_ID and a.month=1 and b.month=2
方法二:update price as a,(select * from price where month=2) as b set a.price=b.price where a.r_ID=b.r_ID and a.month=1
(7)mysql 下sql語(yǔ)句 update 字段=字段+字符串 表字段某一個(gè)原值加一個(gè)字符串
create table a
(
id VARCHAR(40),
phone VARCHAR(40),
email VARCHAR(40)
)
insert into a VALUES (1,18078526042,’’);
insert into a VALUES (2,18078526043,’’);
insert into a VALUES (3,18078526044,’’);
1、要求將所有email為空的字段值改成phone連接字符串@qq.com的格式
#這是單獨(dú)執(zhí)行一條數(shù)據(jù)
SELECT * FROM a where id=1;
update a set email=concat(phone,’@qq.com’) where id=‘1’;
#批量執(zhí)行多條數(shù)據(jù)
SELECT * FROM a where email=’’;
update a set email=concat(phone,’@qq.com’) where email=’’;
提醒:mysql下sql語(yǔ)句令某字段值等于原值加上一個(gè)字符串
update 表明 SET 字段= ‘feifei’ || 字段; (postgreSQL 用 || 來(lái)連貫字符串)
MySQL連貫字符串不能利用加號(hào)(+),而利用concat。
總結(jié)
以上是生活随笔為你收集整理的mysql语法之update的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 麒麟V10系统密码策略修改
- 下一篇: linux cmake编译源码,linu