我正在尝试为下面的逻辑编写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游标中的记录数?
答案 0 :(得分:12)
在光标前进行计数:
select count(*)
into @user_cnt
from users
where fullname like concat (
lastname,
' ',
firstname,
' (',
middlename,
'%'
);
编辑: 如果您想在打开光标后执行此操作,请尝试执行以下操作:
OPEN your_cursor;
select FOUND_ROWS() into user_cnt ;
答案 1 :(得分:0)
在游标选择中包含SQL_CALC_FOUND_ROWS。然后在获取时使用FOUND_ROWS()。它给出了记录数