【一条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



大神语录:

 结果不重要,原理要清楚,where的过滤对象是表(包括临时表),rownum是你的临时变量,要么就把这个变量放到临时表了,如@三分后七分 所做的那样,要么就用having过滤,因为having过滤的是返给客户端的数据。 



方法二:

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

Logo

更多推荐