mysql 保存 union_在MYSQL存儲過程中將UNION查詢的結果保存在局部變量中

In my MySQL Stoared Procedure I want to store result of bellow query into local variables.

在我的MySQL Stoared過程中,我想將波紋管查詢的結果存儲到局部變量中。

MySQL SP

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;

How can I pass result of query into local variable? note : above SP will return only 1 row.

如何將查詢結果傳遞給局部變量?注意:上面的SP只返回1行。

1 个解决方案

#1

3

You can achieve this without having to store the value into a variable.

您無需將值存儲到變量中即可實現此目的。

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

But, if you want to store a value for later use, you can use the INTO keyword:

但是,如果要存儲值以供以后使用,可以使用INTO關鍵字:

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值