
通过rownum实现即可。
sql:select rownum , from tablename where 条件语句。
解释:rownum是隐藏的,查询结果默认从1开始编号,所以肯定会是自然编号的,有多少条,编号就到多少。
第一个办法:用ROW_NUMBER() OVER(ORDER BY 你原来排序的方式 );
第二个方法:增加一同值的列,用来order by,例如:
select row_number()over(order by orderid),t1 from (select 1 as orderid,t from 表 t)t1;
第三个办法:使用Identity+临时表,例如:
select Identity(int,1,1),t Into #temptable from 表 t;
select from #temptable;
mysql>
select
from
a;
+-----+
|
col
|
+-----+
|
0
|
|
1
|
|
2
|
|
3
|
|
4
|
|
5
|
|
6
|
|
7
|
+-----+
8
rows
in
set
(000
sec)
mysql>
set
@i
:=
0;
select
@i
:=
@i
+
1
as
`order`,
a
from
a
order
by
col
desc;
+-------+-----+
|
order
|
col
|
+-------+-----+
|
1
|
7
|
|
2
|
6
|
|
3
|
5
|
|
4
|
4
|
|
5
|
3
|
|
6
|
2
|
|
7
|
1
|
|
8
|
0
|
+-------+-----+
8
rows
in
set
(000
sec)
mysql>
以上就是关于怎样在SQL的一个查询中增加一个序列号全部的内容,包括:怎样在SQL的一个查询中增加一个序列号、sql2008 关于查询数据时,添加一个自增序列号的列的问题、mysql 排序后, 如何加上序列号等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)