I am trying to write mysql procedure for below logic,
select id, fullname from users where fullname like concat(lastname, ' ', firstname, ' (' , middlename, '%');
if above query returns 0 records then
select id, fullname from users where fullname like concat(lastname, ' ', firstname, '%');
.... few more queries depending upon result,
I am trying to write procedure mysql procedure , in that, I am using mysql cursor,
DECLARE user_cnt CURSOR FOR select id, fullname from users where fullname like concat(lastname, ' ', firstname, ' (' , middlename, '%');
now I want to check number of records in user_cursor so that I can check the condition "if query returns 0 records then"
How can I find the number of records in mysql cursor without iterating on it ?
Do a count before the cursor:
select count(*)
into @user_cnt
from users
where fullname like concat (
lastname,
' ',
firstname,
' (',
middlename,
'%'
);
EDIT: If you want to do it after OPENING the cursor, try doing:
OPEN your_cursor;
select FOUND_ROWS() into user_cnt ;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With