我正在尝试使用嵌套循环外层访问所有类别,然后使用那个taxID查询该税收中的帖子,以便可以根据其类别位置为其分配排名。
但是我面临的问题是,如果我使用Repeat代替RankLoop然后是每个taxID的第一条记录,则内部循环“ RankingLoop”仅访问一个taxID的所有发布结果,然后终止循环并且不为其他类别的帖子循环循环变得可访问。任何机构都可以帮助解释我的代码中的问题吗?
谢谢您的帮助。
DELIMITER $$
CREATE PROCEDURE Catranking__()
Begin
Declare txID INT; Declare txmaxcount INT;
Declare txCount CURSOR FOR SELECT max(term_taxonomy_id) FROM `wp_term_taxonomy` where taxonomy = 'category' and count > 0;
Declare taxonomyIDS CURSOR FOR SELECT term_taxonomy_id FROM `wp_term_taxonomy` where taxonomy = 'category' and count > 0;
open taxonomyIDS; open txCount;
FETCH txCount into txmaxcount;
loop_label: LOOP
FETCH taxonomyIDS into txID;
if txID < (txmaxcount+1) then set @rank=0;
categorywisePostSorting: begin
DECLARE Vranking INT;
DECLARE VPOSTID INT;
Declare RankingArray CURSOR FOR select @rank:=@rank+1 as ranking, POSTID FROM RankingView WHERE term_id = txID order by claimedProducts desc, commentcount desc, pageviews desc;
open RankingArray;
RankingLoop : Loop
FETCH RankingArray into Vranking, VPOSTID;
select VPOSTID;
end Loop RankingLoop;
close RankingArray;
END categorywisePostSorting;
end if;
END LOOP loop_label;
close taxonomyIDS; close txCount;
END$$
DELIMITER ;
答案 0 :(得分:0)
由于逻辑未知,请进行如下修改
DELIMITER $$
CREATE PROCEDURE Catranking__()
BEGIN
DECLARE txmaxcount INT;
DECLARE txID INT;
DECLARE done1 BOOLEAN DEFAULT FALSE;
# assuming `count` is column name so instead of cursor we can use a local var
#Declare txCount CURSOR FOR SELECT max(term_taxonomy_id) FROM `wp_term_taxonomy` where taxonomy = 'category' and count > 0; -- "count" is illegal here(whether its column name or count(*))... so check from ur side
DECLARE taxonomyIDS CURSOR FOR SELECT term_taxonomy_id FROM `wp_term_taxonomy` WHERE taxonomy = 'category' AND count > 0; -- same count issue
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done1=TRUE;
SELECT
MAX(term_taxonomy_id)
INTO txmaxcount FROM
`wp_term_taxonomy`
WHERE
taxonomy = 'category' AND `count` > 0;
OPEN taxonomyIDS;
TAXONOMYIDS_LOOP: LOOP
FETCH taxonomyIDS INTO txID;
IF done1 THEN
LEAVE TAXONOMYIDS_LOOP;
END IF;
IF txID < (txmaxcount+1) THEN
SET @rank=0;
CATEGORYWISEPOSTSORTING: BEGIN
DECLARE Vranking INT; -- why this var? hasn't used in the code
DECLARE VPOSTID INT;
DECLARE done2 BOOLEAN DEFAULT FALSE;
DECLARE RankingArray CURSOR FOR
SELECT @rank:=@rank+1 AS ranking, POSTID FROM RankingView WHERE term_id = txID ORDER BY claimedProducts DESC, commentcount DESC, pageviews DESC;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done2=TRUE;
OPEN RankingArray;
RANKINGLOOP : LOOP
FETCH RankingArray INTO Vranking, VPOSTID;
IF done2 THEN
LEAVE RANKINGLOOP;
END IF;
SELECT VPOSTID;
END LOOP RANKINGLOOP;
CLOSE RankingArray;
SET done2= FALSE;
END CATEGORYWISEPOSTSORTING;
END IF;
END LOOP TAXONOMYIDS_LOOP;
CLOSE taxonomyIDS;
SET done=FALSE;
END$$
DELIMITER ;