在MYSQL存储过程中将UNION查询的结果保存在局部变量中

时间:2012-02-10 12:13:48

标签: mysql stored-procedures union

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

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行。

1 个答案:

答案 0 :(得分: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