關於MYSQL替換和存儲過程的問題、mysql
業務場景是源數據排序問題、致使終端節目排序混亂、以前約定的排序字段sequence全是0,因而想到用節目標題中的數字來排序、如「甄說紅樓之15—劉姥姥得濟出榮府」、中獲取數字1五、可是mysql中進行數值匹配不太熟、因而記錄下git
中間用到以前沒用過的字符串截取問題
sql
一、從左開始截取字符串
left(str, length)
說明:left(被截取字段,截取長度)
例:select left(content,200) as abstract from my_content_t
二、從右開始截取字符串
right(str, length)
說明:right(被截取字段,截取長度)
例:select right(content,200) as abstract from my_content_t
三、截取字符串
substring(str, pos)
substring(str, pos, length)
說明:substring(被截取字段,從第幾位開始截取)
substring(被截取字段,從第幾位開始截取,截取長度)
例:select substring(content,5) as abstract from my_content_t
select substring(content,5,200) as abstract from my_content_t
(注:若是位數是負數 如-5 則是從後倒數位數,到字符串結束或截取的長度)
四、按關鍵字截取字符串
substring_index(str,delim,count)
說明:substring_index(被截取字段,關鍵字,關鍵字出現的次數)
例:select substring_index("blog.jb51.net","。",2) as abstract from my_content_t
結果:blog.jb51
(注:若是關鍵字出現的次數是負數 如-2 則是從後倒數,到字符串結束)this
最後結果.net
UPDATE HFProgramDataDigital p INNER JOIN ( SELECT id, SUBSTRING(title, LOCATE('之', title)+1,LOCATE('—', title)-LOCATE('之', title)-1 ) AS sq FROM HFProgramDataDigital WHERE pdid=86089) a ON a.id=p.id SET sequence=a.sq
開始頭腦發熱用存儲過程、也貼一下
code
DELIMITER $$ USE `APIDB`$$ DROP PROCEDURE IF EXISTS `testpro`$$ CREATE DEFINER=`root`@`%` PROCEDURE `testpro`() BEGIN DECLARE this_id INT(10); DECLARE this_title VARCHAR(255); DECLARE done INT DEFAULT FALSE; DECLARE cur CURSOR FOR SELECT id,title FROM HFProgramDataDigital WHERE pdid=86089 ; DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=TRUE; OPEN cur; REPEAT FETCH cur INTO this_id,this_title; UPDATE HFProgramDataDigital SET sequence = SUBSTRING(title, LOCATE('之', title)+1,LOCATE('—', title)-LOCATE('之', title)-1 ) WHERE id=this_id; SELECT this_id; UNTIL done END REPEAT; CLOSE cur; END$$ DELIMITER ;
記錄一下啊、備註一下blog