本文介绍了我如何添加一个新列来将行数作为序列号进行计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

record of
id  fare    commission  routecode   vehicle number  productcode date    time    driver  owner name
15  12345   123 4533    1   3344    2011-03-18  00:00:00    yasir   saleem
20  a   a   3433    1   2333    2011-03-25  00:00:00    yasir   saleem
36  11111   11111   3433    1   2333    2011-03-25  16:13:12    yasir   saleem
9   1233    123 3433    nk-234  2333    2011-03-24  00:00:00    siddiq  aslam
21  1200    120 4533    nk-234  7655    2011-03-24  00:00:00    siddiq  aslam
22  1200    133333  0987    nk-234  2333    2011-03-11  00:00:00    siddiq  aslam
23  10000   11  4533    nk-234  7655    2011-03-19  00:00:00    siddiq  aslam
25  122 12  0987    nk-234  2333    2011-03-11  00:00:00    siddiq  aslam
26  1000    100 3344    nk-234  7655    2011-03-11  00:00:00    siddiq  aslam
27  1000    100 3344    nk-234  2333    2011-03-10  00:00:00    siddiq  aslam
34  100 10  3344    nk-234  2333    2011-03-18  00:00:00    siddiq  aslam
35  100 10  3344    nk-234  2333    2011-03-02  00:00:00    siddiq  aslam
5   1000    100 1234    wq1233  3344    2011-03-10  22:30:00    waqas   sami
6   2222    22  1234    wq1233  3344    2011-03-17  22:30:00    waqas   sami
24  a   a   4533    PSS-1234    7655    2011-03-02  00:00:00    salman  salam
42633   145175

我想在id之前添加另一列,以计算

I want to add another column before id which counts the number of

行.它应该从1开始,然后每行增加1.

rows. It should start from 1 and increment by 1 for each row.

推荐答案

如果在SELECT语句中表示:

If you mean in a SELECT statement:

说您的选择是

select * from tbl

它变成

select @n := @n + 1 RowNumber, t.*
from (select @n:=0) initvars, tbl t

注意:

  1. select @n:=0用于将全局变量重置为0
  2. @n := @n + 1每行从1开始将其增加1.此列名为"RowNumber"
  1. select @n:=0 is used to reset the global variable to 0
  2. @n := @n + 1 increases it by 1 for each row, starting from 1. This column is named "RowNumber"

这篇关于我如何添加一个新列来将行数作为序列号进行计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 08:58