【发布时间】:2010-09-03 15:37:17
【问题描述】:
我有以下存储过程:
proc_main:begin
declare done tinyint unsigned default 0;
declare dpth smallint unsigned default 0;
create temporary table hier(
AGTREFERRER int unsigned,
AGTNO int unsigned,
depth smallint unsigned default 0
)engine = memory;
insert into hier values (p_agent_id, p_agent_id, dpth);
/* http://dev.mysql.com/doc/refman/5.0/en/temporary-table-problems.html */
create temporary table tmp engine=memory select * from hier;
while done <> 1 do
if exists( select 1 from agents a inner join hier on a.AGTREFERRER = hier.AGTNO and hier.depth = dpth) then
insert into hier
select a.AGTREFERRER, a.AGTNO, dpth + 1 from agents a
inner join tmp on a.AGTREFERRER = tmp.AGTNO and tmp.depth = dpth;
set dpth = dpth + 1;
truncate table tmp;
insert into tmp select * from hier where depth = dpth;
else
set done = 1;
end if;
end while;
select
a.AGTNO,
a.AGTLNAME as agent_name,
if(a.AGTNO = b.AGTNO, null, b.AGTNO) as AGTREFERRER,
if(a.AGTNO = b.AGTNO, null, b.AGTLNAME) as parent_agent_name,
hier.depth,
a.AGTCOMMLVL
from
hier
inner join agents a on hier.AGTNO = a.AGTNO
inner join agents b on hier.AGTREFERRER = b.AGTNO
order by
-- dont want to sort by depth but by commission instead - i think ??
-- hier.depth, hier.agent_id;
a.AGTCOMMLVL desc;
drop temporary table if exists hier;
drop temporary table if exists tmp;
end proc_main
虽然该函数做得很好 - 它目前只允许通过 AGTCOMMLVL 降序排序。存储过程的目的是将 memberID 与其 parentID 和关联的 COMMLVL 匹配。一旦正确配对,我在第二个查询中使用 memberID 来返回有关该特定成员的信息。
我希望能够按任意数量的过滤器进行排序,但有以下问题:
我似乎找不到将变量传递到存储过程以更改其按字段排序的方法。
即使我可以 - 排序实际上可能只包含来自第二个查询的数据(例如名字、姓氏等)
即使语法正确,在第二个查询中运行排序也无济于事 - 它总是退回到存储过程的排序。
有什么想法吗?
编辑
我的 php 使用 mysqli 和代码:
$sql = sprintf("call agent_hier2(%d)", $agtid);
$resulta = $mysqli->query($sql, MYSQLI_STORE_RESULT) or exit(mysqli_error($mysqli));
【问题讨论】:
标签: php mysql stored-procedures sorting