我想以排序的方式将id添加到我的表中

时间:2016-03-19 22:00:20

标签: sql oracle sqlite

我的表就像这样

   release
   b
   c
   a
   e
   f
   q

我希望按照从a到z的排序方式添加id,就像这样

   release    id
   b          2
   c          3
   a          1
   e          4
   f          5
   q          6

怎么做?

1 个答案:

答案 0 :(得分:2)

Oracle查询

MERGE INTO table_name d
USING ( SELECT ROWID,
               ROW_NUMBER() OVER ( ORDER BY release ) AS id
        FROM table_name ) s
ON ( d.ROWID = s.ROWID )
WHEN MATCHED THEN UPDATE SET id = s.id;

Oracle和SQLite查询

UPDATE table_name
SET id = (SELECT COUNT(*)
          FROM   table_name b
          WHERE  table_name.release >= b.release);

SQLFIDDLE