旧查询
对于 SQL Server:
SELECT ROW_NUMBER()
OVER (ORDER BY ShiptoID) AS Row,
ShiptoID
FROM YourShip
对于 MYSQL:
SELECT s.shiptoid,
@rownum := @rownum + 1 AS Row
FROM yourship s,
(SELECT @rownum := 0) r
对于甲骨文:
SELECT ShiptoID, ROWNUM AS Row,
ShiptoID
FROM YourShip
更新的查询
对于 MySQL: * SQLFIDDLE:忽略同一页面中的其他表。
询问:
select y.shiptoID, y.row
from (select shiptoID,
@row:=if(@x=shiptoID,@row,0) + 1 as row,
@x:=shiptoID
from yourship
order by shiptoID, row asc
)as y
结果:
SHIPTOID ROW
642218 1
642218 2
642218 3
3041077 1
3041077 2
3041340 1
对于 Oracle:Oracle 有RANK()
,DENSE_RANK()
OVER PARTITION
类似于 MYSQL。
所以我们可以在 ORACLE 中使用类似于 MYSQL 的查询。但最好的部分是,我ROWNUM
在ORDER BY
子句中使用了;)所以说最简单的逻辑/语法在所有 3
参考:* SQLFIDDLE
询问:
SELECT shiptoID,
DENSE_RANK() OVER
(PARTITION BY shiptoID ORDER BY rownum) AS row
FROM yourShip
;
结果:
SHIPTOID ROW
642218 1
642218 2
642218 3
3041077 1
3041077 2
3041340 1