2012-02-10 14 views
1

MySQL Stoaredプロシージャでは、ローカル変数にベロークエリの結果を格納します。UNIONクエリの結果をMYSQLストアドプロシージャのローカル変数に保存

MySQLのSP Iは、ローカル変数にクエリの結果を渡すことができますどのように

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

関連する問題