MySQL For Each 具有特定值的条件

标签 mysql stored-procedures cursor

我正在尝试使用从另一个查询接收到的一组值来调用存储过程,并且我想知道如何使用查询中的值调用另一个过程。这是我的代码

CREATE DEFINER=`root`@`localhost` PROCEDURE `temp`(IN u_id int)
BEGIN

#below query will give me all the u_id values that i need to use(ex : 2,8,9) 
Declare cur cursor for select r_id from temp.usr_rl where u_id in (u_id);

#below i would like to use the u_id values and run the below procedure in a loop for each value in u_id
open cur;
repeat
   fetch cur into a;
    if not done then
      call get_r(a);
    end if;
until done end repeat;    

close cur;
END 

最佳答案

这会导致巨大的性能损失,因为您使用游标来处理每条记录并调用另一个过程。因此,您实际上使效果加倍。相反,使用如下所示的 CTAS 获取并存储 临时表 中的所有值,并从过程中调用 get_r 访问该临时表以进行任何操作您正在进行的进一步处理。

create temporary table myTemp 
as select r_id from temp.usr_rl where u_id = u_id;

关于MySQL For Each 具有特定值的条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41186625/

相关文章:

导入sql文件时MySQL 1064错误

php - 需要密码才能访问页面

java代码使存储过程在DAO层返回结果集

mysql - SQL Server中如何查询相似度百分比?

python - 类型错误 : tuple indices must be integers, 不是 str Python/django

java - 如何在 GWT TextArea 中启用光标选择器,同时禁用文本修改?

php - 获取id并更新数据库

mysql查询: file sort when inner join, limit and order by

mysql - 在单个 sql 语句中获取多个条件的计数和总和

mysql - 错误声明游标mysql存储过程