请教如何取得mysql 排序的序号

请教如何取得mysql 排序的序号,第1张

mysql>select * from a

+-----+

| col |

+-----+

| 0 |

| 1 |

| 2 |

| 3 |

| 4 |

| 5 |

| 6 |

| 7 |

+-----+

8 rows in set (0.00 sec)

mysql>set @i := 0select @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>

用mysql查询后,怎样生成名次的顺序号

SELECT 语句中的 Order By 子句,决定返回记录集合的排序方式

例如:

按照【名次】值,从小到大排序

Select * From 表名 Where ...... Order By 名次

按照【名次】值,从大到小排序

Select * From 表名 Where ...... Order By 名次 Desc

这样不就行吗

创建测试表及插入数据

create table a

(顾客id  int,

消费id int,

消费金额 int)

insert into a values (1,1,100)

insert into a values (1,2,20)

insert into a values (1,3,300)

insert into a values (1,4,40)

执行

set @r=0

select @r:=@r+1, a.*

 

from A

 

where 顾客id=1

结果

其中第一列就是消费次数


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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存