假设我有一个如下所示的表:
CREATE TABLE animal
(
name VARCHAR (50),
);
INSERT INTO animal VALUES ('Cat');
INSERT INTO animal VALUES ('Dog');
INSERT INTO animal VALUES ('Elephant');
是否有一种方法来放置一个自动递增列在这样的方式,结果如下?
1,Cat
2,Dog
3,Elephant
可以使用Row_Number()
SELECT ROW_NUMBER() OVER (ORDER BY [name]) RowNum, [name]
FROM animal;
您需要row_number()
:
select row_number() over(order by (select null)) as id, name
from animal;