在mysql

时间:2015-07-21 10:00:05

标签: mysql database

我在每个简单的Mysql查询中花了好几个小时(搜索和尝试)。我试图将一个变量从一个select语句传递给next语句。但我被抛出一排空行。

SET @id :='SELECT MAX(sId) from surveys';
SELECT typeId as type from surveys WHERE sId=@id;
PREPARE stmt from @id;
execute stmt;

我尝试执行SELECT MAX(sId) from surveys,根据需要提供结果。但似乎@id不接受这些价值观。是否有任何错误或任何其他建议来解决这个问题?

1 个答案:

答案 0 :(得分:0)

尝试这个:

SET @s = 'SELECT typeId as type from surveys WHERE sId=?';
PREPARE stmt from @s;
SET @id = (SELECT MAX(sId) from surveys);
EXECUTE stmt USING @id;

DEALLOCATE PREPARE stmt;