【一条sql语句完成,不set变量】
SELECT @rownum := @rownum+1 AS rownum,
table1.*
FROM (SELECT @rownum:=0) r ,
table1;
oracle: SELECT T.*,rownum FROM (SELECT BMBH,CC,SSBMBH FROM ZC_BM WHERE SFSY=1
START WITH BMBH='000215'
CONNECT BY PRIOR BMBH=SSBMBH
ORDER BY CC DESC,BMBH) T WHERE rownum=1
执行结果:
改成 mysql
方法一:
SELECT T.*,@rownum := @rownum+1 AS rownum FROM (SELECT @rownum:=0) r ,(
SELECT BMBH,CC,SSBMBH FROM ZC_BM WHERE FIND_IN_SET(BMBH, getChildList('000215')) ORDER BY CC DESC,BMBH
) T having rownum=1
大神语录:
方法二:
select *
from(
select t.*,@rownum:=@rownum+1 rownum
from(select @rownum:=0)r,( SELECT BMBH,CC,SSBMBH FROM ZC_BM WHERE FIND_IN_SET(BMBH, getChildList('000215')) ORDER BY CC DESC,BMBH) t)c
where c.rownum=1