1

I have a query that returns 0 records however should not. My query is,

SET @id = (SELECT name FROM tag_type WHERE id = 1);
SET @s = CONCAT('SELECT * FROM ', @id);
PREPARE stmt1 FROM @s;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;

MySQL Tells me that:

SET @id := (SELECT DISTINCT name FROM tag_type WHERE id = 1);# MySQL returned an empty result set (i.e. zero rows).

SET @s = CONCAT('SELECT * FROM ', @id);# MySQL returned an empty result set (i.e. zero rows).

PREPARE stmt1 FROM @s;# MySQL returned an empty result set (i.e. zero rows).

EXECUTE stmt1;# Rows: 366

DEALLOCATE PREPARE stmt1;# MySQL returned an empty result set (i.e. zero rows).

However if I run the query on it's own as:

SELECT name FROM tag_type WHERE id = 1;

Then I get the results expected. Why is this? Thanks, Nick

2
  • Are you connected to the same database when you run the two queries? Also, you don't need := for set. Dec 21, 2014 at 15:52
  • Yes, I am and I have tried both with and without : Dec 21, 2014 at 15:54

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.