我有一个返回0条记录但不应该返回的查询。我的查询是,
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告诉我:
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).
但是,如果我在其上运行查询,请将其作为:
SELECT name FROM tag_type WHERE id = 1;
然后我得到预期的结果。为什么是这样?谢谢,尼克