Select ID = (Select Count(userid) From A Where userid <= T.userid), * From A T

解决方案 »

  1.   

    select (select count(*) from A tmp where tmp.userid<=A.userid ) as no,* from A order by userid
      

  2.   

    Create Table A
    (ID Int Identity(1, 1),
     IText Varchar(10))
    GO
    Insert A Select '01'
    union All Select '02'Select Max(IText) From A
    Select SUM(Cast(IText As Int)) From A
    GO
    Drop Table A表 
    Select Max(字段) From 表
    Select Max(Cast(字段 As Int)) From A
    Select SUM(Cast(字段 As Int)) From A
    GO现有数据库中A表,该表包含userid(主键)、username、pssword三个字段,
    现有数据
    001     liu     123
    003     wang    123
    008     zhang   456
    009     xia     785现在要做查询查询结果如下:1  001     liu     123
    2  003     wang    123
    3  008     zhang   456
    4  009     xia     785想问这个查询该怎么做?不可以建临时表,只可要sql语句。数据库是sqlServer2000
    Select ID = (Select Count(userid) From A Where userid <= T.userid), * From A T
    Create Table A
    (userid Char(3) Primary Key,
     username Varchar(20),
     pssword Varchar(20))
    Insert A Select '001',     'liu',        '123'
    Union All Select '003',     'wang',    '123'
    Union All Select '008',     'zhang',   '456'
    Union All Select '009',     'xia',        '785'
    GO
    Select ID = (Select Count(userid) From A Where userid <= T.userid), * From A T
    GO
    Drop Table A
    --Result
    /*
    ID userid username pssword
    1 001 liu 123
    2 003 wang 123
    3 008 zhang 456
    4 009 xia 785
    */
      

  3.   

    暈,貼多了。
    Create Table A
    (userid Char(3) Primary Key,
     username Varchar(20),
     pssword Varchar(20))
    Insert A Select '001',     'liu',        '123'
    Union All Select '003',     'wang',    '123'
    Union All Select '008',     'zhang',   '456'
    Union All Select '009',     'xia',        '785'
    GO
    Select ID = (Select Count(userid) From A Where userid <= T.userid), * From A T
    GO
    Drop Table A
    --Result
    /*
    ID userid username pssword
    1 001 liu 123
    2 003 wang 123
    3 008 zhang 456
    4 009 xia 785
    */