select *,id=(select count(1)+1 from tb where user_id<a.user_id)
from tb a
这条句子中的  where user_id<a.user_id是怎么比较的 user_id和a.user_id不是应该相同的吗?为什么带上这条where后id的之就是1,2,3,4,5,6,7.....
-------------------------------------------
自学很累,有时候很简单的问题要看很长时间,希望大家帮帮忙

解决方案 »

  1.   

    select *,id=(select count(1)+1 from tb where user_id<a.user_id) from tb aselect *,id=(select count(1) from tb where user_id<a.user_id) + 1 from tb a意思就是通过子查询比较user_id 和 t(别名) 表 user_id 小的记录数.
      

  2.   

    还是不太明白 希望各位高人能耐心解释一下
    这个比较是不是 不管user_id列的值是什么 最后id的只都是从1开始递增的?
      

  3.   

    刚开始学SQL时我也对这个很有疑惑,历经多次问答后才慢慢理解消化。主要就是通过比较子查询的user_id比t.user_id小的行数。
      

  4.   

    --这样能看懂吗?
    create table tb([user_id] int)
    insert into tb values(10)
    insert into tb values(20)
    insert into tb values(30)
    insert into tb values(40)
    insert into tb values(50)
    goselect t.[user_id] , 
           (select count(1) from tb where [user_id] < t.[user_id]) ,
           (select count(1) + 1 from tb where [user_id] < t.[user_id]) ,
           (select count(1) from tb where [user_id] < t.[user_id]) + 1
    from tb tdrop table tb/*
    user_id                                         
    ----------- ----------- ----------- ----------- 
    10          0           1           1
    20          1           2           2
    30          2           3           3
    40          3           4           4
    50          4           5           5(所影响的行数为 5 行)*/
      

  5.   

    如果user_id从小到大,不重复但可以是断号,结果就是得到一列的顺序号。
      

  6.   

    有关序号或名次的其他内容见下:
    表jh03有下列数据:
    name score
    aa  99
    bb  56
    cc  56
    dd  77
    ee  78
    ff  76
    gg  78
    ff  501. 名次生成方式1,Score重复时合并名次
    SELECT * , Place=(SELECT COUNT(DISTINCT Score) FROM jh03 WHERE Score >= a.Score)
    FROM jh03 a
    ORDER BY Place
    结果
    Name       Score        Place 
    ---------------- ----------------- ----------- 
    aa         99.00        1
    ee         78.00        2
    gg         78.00        2
    dd         77.00        3
    ff         76.00        4
    bb         56.00        5
    cc         56.00        5
    ff         50.00        62. 名次生成方式2 , Score重复时保留名次空缺
    SELECT * , Place=(SELECT COUNT(Score) FROM jh03 WHERE Score > a.Score) + 1
    FROM jh03 a
    ORDER BY Place
    结果
    Name       Score        Place 
    --------------- ----------------- ----------- 
    aa         99.00        1
    ee         78.00        2
    gg         78.00        2
    dd         77.00        4
    ff         76.00        5
    bb         56.00        6
    cc         56.00        6
    ff         50.00        8
      

  7.   

    create table tb([user_id] int)
    insert into tb values(10)
    insert into tb values(20)
    insert into tb values(20)
    insert into tb values(40)
    insert into tb values(50)
    goselect t.[user_id] , 
           (select count(1) from tb where [user_id] < t.[user_id]) ,
           (select count(distinct [user_id]) + 1 from tb where [user_id] < t.[user_id]) ,
           (select count(1) from tb where [user_id] <= t.[user_id]) 
    from tb t
    /*
    ----------- ----------- ----------- -----------
    10          0           1           1
    20          1           2           3
    20          1           2           3
    40          3           3           4
    50          4           4           5(5 行受影响)
    */
    drop table tb再比较三个
      

  8.   

    首先谢谢大家的解答  可是我不明白的是user_id<a.user_id  这两个值  具体是哪两个在做比较??
      

  9.   

    create table tb([user_id] int)
    insert into tb values(10)
    insert into tb values(20)
    insert into tb values(30)
    insert into tb values(40)
    insert into tb values(50)
    go
    select *,(select COUNT(1) from tb t where t.[user_id]<=tb.[user_id]) from tb
     --等效于
    select a.[user_id] ,COUNT(1)
    from tb a
    left join tb b
    on a.[user_id]>=b.[user_id]
    group by a.[user_id]
    /*
    user_id     
    ----------- -----------
    10          1
    20          2
    30          3
    40          4
    50          5(5 行受影响)
    */
    drop table tb 
      

  10.   

    是第一条数据的user_id和别名中的第一条user_id相比 然后返回1???
    然后第二条数据的user_id和别名中的第二条user_id相比 然后返回2???
    然后第三条数据的user_id和别名中的第三条user_id相比 然后返回3???我好像不太明白这条句子的执行顺序!
      

  11.   

    create table tb([user_id] int)
    insert into tb values(20)
    insert into tb values(10)
    insert into tb values(50)
    insert into tb values(40)
    insert into tb values(30)
    goselect t.[user_id] , 
           (select count(1) from tb where [user_id] < t.[user_id]) ,
           (select count(distinct [user_id]) + 1 from tb where [user_id] < t.[user_id]) ,
           (select count(1) from tb where [user_id] <= t.[user_id]) 
    from tb t
    20
    1 2 2 10
    0 1 1 50
    4 5 5 40
    3 4 4 30
    2 3 3
      

  12.   

    CTRL+U,查看执行计划就明白了,你可以按你的那样的理解,先从外表查询一条值,然后与那表进行比较返回行数
      

  13.   

    第一条数据的user_id和别名中的所有user_id相比 然后返回1
    第二条数据的user_id和别名中的所有user_id相比 然后返回2
    第三条数据的user_id和别名中的所有user_id相比 然后返回3
      

  14.   

    后面的子查询是独立的  他查询的条件就是他的ID比你前面的ID小
      

  15.   

    select *,id=(select count(1)+1 from tb where user_id<a.user_id)
    from tb a表查询实际就是根据条件,对逐条记录进行筛选.
    对以上的查询
    主查询扫描tb a,tb表被赋一别名 a,
    开始扫描
    第一条记录,假设此记录userid=5
    * 取出此记录的全部字段,
      关键在于对子查询 id=(select count(1)+1 from tb where user_id<a.user_id)
     对第一条记录, 将userid=5代入子查询得
          select count(1)+1 from tb where user_id<5 此查询结果即为id列的输出结果
    对第二条记录,将第二条记录的user_id值代入子查询,得第二条记录的id结果,
    第三条......到最后一条记录,都用相同的方法得出id列的值
    -------------------
      难理解的可能是子查询,不要把子查询的tb与主查询的tb相混,
      子查询可理解为请别人按条件查询tb表,应理解为单独的行为,与统计其它表的道理相同.
    希望以上解释能帮助LZ理解.还有什么问题,请回帖