2012-02-10 69 views
1

在我的MySQL Stoared过程中我想将波纹管查询的结果存储到局部变量中。在MYSQL存储过程中将UNION查询的结果保存在本地变量中

MySQL的SP

BEGIN 
Declare temp_ID bigint; 
Declare temp_teamName Text; 
(select ID,team1 from tbl_tournament_matches where leveID = 1 and tournamentID = 91 and matchType = 'L') 
UNION 
(select ID,team1 from tbl_tournament_matches where leveID = 2 and tournamentID = 91 and looserTeam is not null) 
ORDER BY RAND() LIMIT 0,1; 
select temp_ID, temp_teamName; 
END; 

如何传递查询的结果为局部变量? 注意:上面的SP将只返回1行。

回答

3

无需将该值存储到变量中即可实现此目的。

SELECT * FROM(

select ID,team1 from tbl_tournament_matches where leveID = 1 and tournamentID = 91 and matchType = 'L' 

UNION 

select ID,team1 from tbl_tournament_matches where leveID = 2 and tournamentID = 91 and looserTeam is not null 
) ORDER BY RAND() LIMIT 0,1 

但是,如果你想存储供以后使用的值,可以使用INTO关键字:

SELECT id, data INTO @x, @y FROM test.t1 LIMIT 1; 

http://dev.mysql.com/doc/refman/5.0/en/select-into.html

相关问题