MySQL
中将 order by
和 union
结合使用的时候(先排序,再组合),此时 order by
后面必须加上 limit
才会有先排序再组合的效果。
但是若这两个 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)