要求 找到mysql數(shù)據(jù)庫中w字段以/cc開頭的行,并且刪除/cc。首先要找到符合要求的行,這很簡單,select 就可以
select * from a where `CODE` like'/cc%'
第二步 更改字段update a set code=substring(code,4,length(code)-3)
Substring 函數(shù) 截取字符串函數(shù)name = substring(name,4,length(name)-3)
Name 是要截取的字段name,4,意思是從第4個字符開始,length(name)-3返回字段-3的字符,就是說刪除前3個字符。
第三步 想辦法把兩句sql結(jié)合起來。
首先想到的 in 語句
update a set code=substring(code,4,length(code)-3)? whereid in
(select id from a where `CODE` like'/dc%')
但是不行 報錯You can't
specify target table for update in FROM clause錯誤的意思是說,不能先select出同一表中的某些值,再update這個表(在同一語句中)。網(wǎng)上說可以在中間再套嵌一個sql 語句,我試了但是不行,好像是5.7以不上支持這樣做。
最后使用inner join語句解決
update a inner join (select id from a where `CODE` like '/cc%') t? on a.id=t.id set code= substring(code,4,length(code)-3)