MySQL存储过程中使用union

MySQL 中将 order byunion 结合使用的时候(先排序,再组合),此时 order by 后面必须加上 limit 才会有先排序再组合的效果。

参考:MySQL中使用union和order by结合使用

但是若这两个 SQL 子句使用同一个参数时,存储过程中的 union 会失效(不能只传入一个参数,在存储过程中使用多次)。

经过多番测试,可以在存储过程传入两个一模一样的参数。这样问题就得到了解决。

存储过程语句:
delimiter $$
create procedure my_pro01(in input1 int,in input2 int)
begin
select
	a.id, a.name, b.math, a.dept
		from student a LEFT JOIN grade b on a.id=b.id having a.id=input1
UNION ALL
select
	a.id, a.name, b.math, a.dept
		from student a LEFT JOIN grade b on a.id=b.id having a.id=input2;
end
$$
调用存储过程
call my_pro01(1,1)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值