本文介绍了mysql行编号重置每一个不同的记录值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题与该问题非常相似自动每个不同列值的编号和重置计数

My question is pretty similar to this one Auto number and reset count for each different column value

除了我无法使其工作.

I have the table record:
ID(autoINC) plate_number
1                A
2                A 
3                A
4                B
5                B
6                C
7                C

我想显示类似这样的内容,并添加其他字段cc:

I want to display something like this adding additional field cc:

I have the table record:
ID(autoINC) plate_number count
1                A         1
2                A         2 
3                A         3
4                B         1
5                B         2
6                C         1
7                C         2

推荐答案

您可以拥有一个相关子查询,该子查询将按顺序对可用作行号的行进行计数.

You can have a correlated subquery which sequentially count the row which can be used as a rownumber.

SELECT  A.ID, 
        A.plate_number, 
        (
            SELECT  COUNT(*)
            FROM    tableName c
            WHERE   c.plate_number = a.plate_number AND
                    c.ID <= a.ID) AS RowNumber
FROM    TableName a

  • SQLFiddle演示
    • SQLFiddle Demo
    • 这篇关于mysql行编号重置每一个不同的记录值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 22:25