MySQL:无法生成动态订单号

时间:2016-02-19 07:32:06

标签: mysql

我无法使用子查询生成订单号。 这里我有一些示例代码,几个表连接。 我只需从此查询中获取订单号。

我得到了正确的结果但是其他一些连接表在更改BookHeadId后得到了Recline(order)为空。

select @rhBookHeadId :=rh.BookHeadId ,rd.BookDetailId,@inrec:=0,(select RecLine from (SELECT spodno.BookDetailId, @inrec:= ifnull(@inrec,0)+1, @inrec as RecLine FROM tblBookDetail spodno where spodno.BookHeadId=@rhBookHeadId and spodno.isActive=1 order by spodno.BookDetailId asc ) temp where temp.BookDetailId=rd.BookDetailId) as RecLine from  tblBookHead rh inner join tblBookDetail rh on rh.BookHeadId=rd.BookHeadId inner join ......;

1 个答案:

答案 0 :(得分:0)

我通过使用mysql中的函数得到了答案。

CREATE DEFINER=`mycura`@`%` FUNCTION `getRecLine`(dBookDetailId bigint(20),
dBookHeadId bigint(20)
) RETURNS int(11)
BEGIN
declare dRecLine int default 0;

set @inrec =0;

select RecLine into dRecLine from (
SELECT spodno.BookDetailId, @inrec:= @inrec+1, @inrec as RecLine 
FROM tblBookDetail spodno where spodno.BookHeadId=dBookHeadId and spodno.isActive=1 order by 
spodno.BookDetailId asc ) temp
where temp.BookDetailId=dBookDetailId;



RETURN dRecLine;
END $$

然后调用该函数。它的工作原理

select getRecLine(rd.BookDetailId,rh.BookHeadId) as  RecLine from  tblBookHead rh inner join tblBookDetail rh on rh.BookHeadId=rd.BookHeadId inner join ......;