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.....
-------------------------------------------
自学很累,有时候很简单的问题要看很长时间,希望大家帮帮忙
from tb a
这条句子中的 where user_id<a.user_id是怎么比较的 user_id和a.user_id不是应该相同的吗?为什么带上这条where后id的之就是1,2,3,4,5,6,7.....
-------------------------------------------
自学很累,有时候很简单的问题要看很长时间,希望大家帮帮忙
这个比较是不是 不管user_id列的值是什么 最后id的只都是从1开始递增的?
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 行)*/
表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
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再比较三个
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
然后第二条数据的user_id和别名中的第二条user_id相比 然后返回2???
然后第三条数据的user_id和别名中的第三条user_id相比 然后返回3???我好像不太明白这条句子的执行顺序!
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
第二条数据的user_id和别名中的所有user_id相比 然后返回2
第三条数据的user_id和别名中的所有user_id相比 然后返回3
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理解.还有什么问题,请回帖