0

我正在尝试使用 Heidisql 使用以下语法在 mysql 中使用简单循环。

BEGIN
loop_label:  LOOP
                     IF  @number_title  > @max THEN
                          **LEAVE  loop_label;**
                     END  IF;
select @number_title as number_distinct_title,count(*) as total from (
select count(distinct ctitle), customer_id
FROM table
GROUP BY customer_id
HAVING count(distinct ctitle)=@number_title 
ORDER BY customer_id) as total ;
SET @number_title = @number_title + 1;
END LOOP
END

我在LEAVE loop_label处收到语法错误;

有人可以帮忙吗?

谢谢

4

1 回答 1

0

在 MySQL 中,当您定义触发器或过程时,通常必须临时更改分隔符,因为触发器/过程中的语句必须以“;”终止。

只需将完整的块括在“--/”和“/”中,你应该会很好

--/
BEGIN
loop_label:  LOOP
                 IF  @number_title  > @max THEN
                      **LEAVE  loop_label;**
                 END  IF;
 select @number_title as number_distinct_title,count(*) as total from (
 select count(distinct ctitle), customer_id
 FROM table
 GROUP BY customer_id
 HAVING count(distinct ctitle)=@number_title 
 ORDER BY customer_id) as total ;
 SET @number_title = @number_title + 1;
 END LOOP
END
 /

这里找到

于 2013-07-03T18:04:55.487 回答