【问题标题】:mysql number of records in cursor without iterating?mysql游标中的记录数没有迭代?
【发布时间】:2013-10-03 20:08:38
【问题描述】:

我正在尝试为以下逻辑编写mysql程序,

select id, fullname from users where fullname like concat(lastname, ' ', firstname, ' (' , middlename, '%');

如果上面的查询返回 0 条记录,那么

    select id, fullname from users where fullname like concat(lastname, ' ', firstname, '%');

.... few more queries depending upon result,

我正在尝试编写程序 mysql 程序,其中,我使用的是 mysql 游标,

DECLARE user_cnt CURSOR FOR select id, fullname from users where fullname like concat(lastname, ' ', firstname, ' (' , middlename, '%'); 

现在我想检查 user_cursor 中的记录数,以便检查条件“如果查询返回 0 条记录”

如何在不迭代的情况下找到mysql游标中的记录数?

【问题讨论】:

    标签: mysql count database-cursor


    【解决方案1】:

    在光标前进行计数:

    select count(*)
    into @user_cnt
    from users
    where fullname like concat (
        lastname,
        ' ',
        firstname,
        ' (',
        middlename,
        '%'
        );
    

    编辑: 如果您想在 OPENING 光标后执行此操作,请尝试执行以下操作:

    OPEN your_cursor;
    select FOUND_ROWS() into user_cnt ;
    

    【讨论】:

    • 我想知道有没有什么方法可以在不迭代的情况下计算现有游标中的记录数?
    • @user1191081。在 OPEN 光标后尝试SELECT FOUND_ROWS()。
    • 最好建议一个没有光标的解决方案作为警告声明,因为性能很糟糕。
    【解决方案2】:

    在您的光标选择中包含 SQL_CALC_FOUND_ROWS。然后在获取时使用 FOUND_ROWS()。它给出了记录的数量

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-05
      • 1970-01-01
      • 1970-01-01
      • 2012-04-03
      • 2014-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多