wcur LOCATE +

时间:2024-12-02 14:36:08

w字符串处理

 DROP PROCEDURE IF EXISTS w_unique;
DELIMITER /w/
CREATE PROCEDURE w_unique()
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE w_wmax INT;
DECLARE w_grab_review_url VARCHAR(256);
DECLARE wcur CURSOR FOR SELECT MAX(grab_tab_review_pk) AS wmax, grab_review_url FROM grab_tab_review WHERE LENGTH(grab_review_url)>0 GROUP BY grab_review_url;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE; OPEN wcur;
read_loop: LOOP
FETCH wcur INTO w_wmax, w_grab_review_url;
IF done THEN LEAVE read_loop;
END IF;
SET @wtmp = CONCAT('DELETE FROM grab_tab_review WHERE grab_tab_review_pk!=',w_wmax,' AND grab_review_url="',w_grab_review_url,'"');
PREPARE stmt FROM @wtmp ;
EXECUTE stmt ;
DROP PREPARE stmt;
END LOOP;
CLOSE wcur;
END/w/
DELIMITER;
CALL w_unique();
 DROP PROCEDURE IF EXISTS w_self_update;
DELIMITER /w/
CREATE PROCEDURE w_self_update(w_arr VARCHAR(36))
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE w_grab_tab_review_pk INT;
DECLARE w_grab_review_url VARCHAR(256);
DECLARE wcur CURSOR FOR SELECT grab_tab_review_pk, grab_review_url FROM grab_tab_review WHERE LENGTH(grab_review_url)>0;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE; OPEN wcur;
read_loop: LOOP
FETCH wcur INTO w_grab_tab_review_pk, w_grab_review_url,w_countrycode;
IF done THEN LEAVE read_loop;
END IF;
SET @w=LOCATE(w_arr,w_grab_review_url);
SET @wb=LOCATE('/ref=',w_grab_review_url);
SET @wstr=SUBSTR(w_grab_review_url,@w + LENGTH(w_arr),@wb - @w - LENGTH(w_arr));
SET @wtmp = CONCAT('UPDATE grab_tab_review ',' SET amz_review_id="',@wstr,'" WHERE grab_tab_review_pk=',w_grab_tab_review_pk);
PREPARE stmt FROM @wtmp ;
IF @w>0 THEN EXECUTE stmt ;
END IF;
DROP PREPARE stmt;
END LOOP;
CLOSE wcur;
END/w/
DELIMITER;
CALL w_self_update('-reviews/');
CALL w_self_update('-review/');
 https://www.amazon.com.mx/review/R1OR2BGFHA44LE/ref=cm_cr_dp_title?ie=UTF8&ASIN=B0196IK0OM&channel=detail-glance&nodeID=9482690011&store=software
https://www.amazon.com.mx/gp/customer-reviews/R1OR2BGFHA44LE/ref=cm_cr_dp_title?ie=UTF8&ASIN=B0196IK0OM&channel=detail-glance&nodeID=9482690011&store=software
===>
R1OR2BGFHA44LE
 SELECT grab_review_url FROM grab_tab_review WHERE grab_tab_review_pk=123456;

 SELECT
SUBSTR(
(
SELECT
grab_review_url
FROM
grab_tab_review
WHERE
grab_tab_review_pk = 123456
),
LOCATE(
'-reviews/',
(
SELECT
grab_review_url
FROM
grab_tab_review
WHERE
grab_tab_review_pk = 123456
)
) + LENGTH('-reviews/'),
LOCATE(
'/ref=',
(
SELECT
grab_review_url
FROM
grab_tab_review
WHERE
grab_tab_review_pk = 123456
)
) - LOCATE(
'-reviews/',
(
SELECT
grab_review_url
FROM
grab_tab_review
WHERE
grab_tab_review_pk = 123456
)
) - LENGTH('-reviews/')
);