例如:在產品表product表中欄位content值為["10"],然後在產品表中新建一個欄位product_id,提出欄位content的值10,如何實現呢? 解: update (select id,substring(content,3,instr(content,"\"]")-3) as p ...
例如:在產品表product表中欄位content值為["10"],然後在產品表中新建一個欄位product_id,提出欄位content的值10,如何實現呢?
解:
update (select id,substring(content,3,instr(content,"\"]")-3) as product_id FROM `product`) b,product a set a.product_id = b.product_id WHERE a.id = b.id
解析:
instr(欄位名, 字元串)——這個函數返回字元串在某一個欄位的內容中的位置, 沒有找到字元串返回0,否則返回位置(從1開始)
substring(被截取欄位,從第幾位開始截取,截取長度)
拓展:
left(被截取的欄位,截取長度)——從左開始截取字元串
right(被截取的欄位,截取長度)——從右開始截取字元串
substring_index(被截取欄位,關鍵字,關鍵字出現的次數)——按關鍵字截取字元串
(註:如果關鍵字出現的次數是負數 如-2 則是從後倒數,到字元串結束)