mysql游标循环选择

标签 mysql loops cursor handler fetch

我正在编写一个程序,它将遍历一个表并收集所有前 10 个结果。我正在使用游标然后获取结果。我对如何打印结果有点困惑。我使用了 select 语句来获得这些结果,但我被告知有错误。

任何帮助将不胜感激。谢谢。

create procedure plant_list()
begin
declare v_plant_id integer(5);
declare v_common_name varchar(30);
declare v_scientific_name varchar(20);


declare finished boolean default false;

declare cur_top_ten cursor for
    select P.plant_id, common_name, concat(genus, ' ', species)
    from plants P
        join plant_taxonomy PT on P.plant_id = PT.plant_id
    order by list_price desc
    limit 10
    ;

declare continue handler for not found set finished = true;

open cur_top_ten;
curloop: loop
    fetch cur_top_ten into v_plant_id, v_common_name, v_scientific_name;
    if finished then
        close cur_top_ten;
        set finished = false;
        leave curloop;
    end if

    select v_plant_id, v_common_name, v_scientific_name;


end loop curloop;

end;
#

最佳答案

if 后面少了一个分号!

  if finished then
    close cur_top_ten;
    set finished = false;
    leave curloop;
  end if;

关于mysql游标循环选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12658087/

相关文章:

javascript - 两个圆圈应该跟随光标——一大一小(有延迟)

django - 每次 .execute() 查询后 django 连接游标是否自动提交?

MySQL - 将自动插入其他表的触发器

php - Magento 将现有评论添加到订单电子邮件

python selenium 在循环中查找子元素

c++ - 字母输入运行无限循环

mysql - 如何从数据库中删除重复项?

mysql - 对于匹配器中的每个选项,确保每个选项至少有一个(但不超过一个)匹配

java - 循环内的变量

mysql - 游标的不一致行为