我MySQL数据库中有两列。 我需要通过跟随另一个字段来识别自动递增
示例:
| id | orgId | createdAt |
--------------------------
| 1 | 1 | {VALUE} |
| 2 | 1 | {VALUE} |
| 3 | 1 | {VALUE} |
--------------------------
| 1 | 2 | {VALUE} |
| 2 | 2 | {VALUE} |
--------------------------
| 1 | 3 | {VALUE} |
| 2 | 3 | {VALUE} |
| 3 | 3 | {VALUE} |
| 4 | 3 | {VALUE} |
| 5 | 3 | {VALUE} |
--------------------------
我需要这样的东西。 id必须按后面自动递增(orgId)字段
您正在查找row_number()
:
select row_number() over (partition by orgId order by orgId) as id,
t.*
from t;
出于您的目的,我不建议存储该值。 取而代之的是:
>
向表中添加自动递增id。
使用上述查询的变体计算所需的序号:
select row_number() over (partition by orgId order by <id col) as id,