Mysql 查询结果新增一列递增序列

Mysql 查询结果新增一列递增序列,第1张

修改sql如下

select (@xh := @xh + 1) as xh, a.* from (select * from auto where level = 3) a, (select @xh := 0) x

结果如下

mysql>

select

*

from

a

+-----+

|

col

|

+-----+

|

0

|

|

1

|

|

2

|

|

3

|

|

4

|

|

5

|

|

6

|

|

7

|

+-----+

8

rows

in

set

(0.00

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

(0.00

sec)

mysql>

序列只有db2

oracle有,mysql没有序列的,不过你可以给你所所创建的表的主键设置为自增。

例如

create

table

A

(

id

int(20)

auto_increment

)

不过设置为自增的键必须是数值类型的。


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5896546.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-07
下一篇 2023-03-07

发表评论

登录后才能评论

评论列表(0条)

保存