如何获取最后的select语句结果集?

时间:2013-09-17 09:32:02

标签: mysql stored-procedures

我有一个类似于Pseudo:

的存储过程
create procedure composite(IN a varchar(255),IN b varchar(255),IN c datetime,IN d datetime)
begin
DECLARE str VARCHAR(255);
DECLARE count float;
SET str = '';
SET str = CONCAT("aname like '%",a,"%' "," and bname ='",b,"' ");
set @comp = CONCAT("SELECT * from abc where ",str, "GROUP BY qname");
PREPARE stmt FROM @comp;
EXECUTE stmt;
set count = found_rows();
SET STR1 = CONCAT("aname like '%",a,"%' "," and bname ='",b,"' ");
SET @sql = CONCAT("SELECT * from xyz",str,"GROUP BY DATE(FROM_UNIXTIME(abcdate)),qname");
PREPARE stmt FROM @sql;
EXECUTE stmt;
end//

当我执行此操作时,我得到2个结果集作为输出,即执行2个select语句。我想要最后一次选择的输出。有没有办法做到这一点?

1 个答案:

答案 0 :(得分:0)

不太明白你想做什么。我想存储过程不完整。

在这种情况下是否真的有必要使用准备好的陈述?你需要count作为浮动吗?

也许这样的事情可能有用:

...
DECLARE count BIGINT UNSIGNED;

SELECT COUNT(*) INTO count /* Here you can use a user variable (@count) and avoiding the local variable (count) */
FROM (
    SELECT 0
    FROM abc
    WHERE aname LIKE CONCAT('%', a, '%') AND bname = b
    GROUP BY qname
) der;

SELECT aname, bname, qname
FROM xyz
WHERE aname LIKE CONCAT('%', a, '%') AND bname = b
GROUP BY qname;
...

第一个语句将存储局部变量count中的行数,并且不会返回数据集。请注意变量名称和保留字,在这种情况下允许使用count,但请在文档中阅读9.3. Reserved Words