【问题标题】:MySQL For Each condition with specific valuesMySQL For Each 条件具有特定值
【发布时间】:2016-12-16 14:33:41
【问题描述】:

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

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 

【问题讨论】:

    标签: mysql stored-procedures cursor


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多