This is a very basic MySQL question but I could not find an answer. Suppose the following storedprocedure body;
DECLARE Existing INT DEFAULT 0;
SELECT SQL_CALC_FOUND_ROWS name FROM users WHERE id = pid LIMIT 1;
SELECT FOUND_ROWS() INTO Existing;
Select Existing;
when I run it it always returns the first SELECT results. However I expect it to return the last (SELECT Existing) results. Is this behaviour normal? If so, how can I change it, because first select is just for checking and the last one is what I need.
(The real logic is different from this, I just simplified it here)
You could do something like this instead:
If
idis found, you’ll return a 1 (id/id = 1). Ifidis not found, you’ll return a 0 (id/id IS NULL and the COALESCE will return the 0).