3_08_MSSQL课程_Ado.Net_子查询

子查询

 1.把一个查询结果作为一个表来使用,就是子查询。

 2.把一个查询结果作为一个 表达式进行使用就是子查询。

 1 --多条查询,上个查询结果放到下面的查询中使用 !!!!子查询
 2 --1.第一种子查询:把一个查询结果,作为一个表来使用
 3 --select myTabel.*
 4 --from(select User_ID,User_Age,User_Pwd,User_Name 
 5 --        from LJK_SqlServerDB.dbo.User_Info
 6 --        where User_Age>1000 and User_Age<10000 and User_ID>1020) as myTabel
 7 --where myTabel.User_Age>1200
 8 
 9 --use LJK_SQLServerDB
10 ----2.第二种子查询:把一个查询结果,作为一个表达式来使用
11 --select * from dbo.User_Info 
12 --where User_Age<(select AVG(User_Age) from User_Info)
 1 --use LJK_SQLServerDB
 2 --select User_Name,sum(User_Pwd)
 3 --from User_Info
 4 --    where User_Pwd>10000
 5 --    group by  User_Name
 6 --    having sum(User_Pwd)>0 
 7 
 8 --use LJK_SQLServerDB
 9 --select * from User_Info
10 --where User_Age in (select User_Age from User_Info group by User_Age having count(1)>0 )
11 
12 --count、 having 、group by等加强认识
13 
14  
15  --分页sql脚本
16  --一页显示3条,显示第4页的数据
17  ----越过多少条(4-1)*3    取多少条 3
18 
19  --第一种写法:
20 -- --select  * from User_Info   order by User_ID
21 
22 --select TOP 3 * from User_Info where User_Id not in 
23 --(
24 --    select top ((4-1)*3) User_Id from User_Info order by User_Id
25 --)                        --越过的数据的id的集合
26 --order by User_ID
27 
28 
29 
30 --第二种写法:
31 --select * 
32 --from (select * ,ROW_NUMBER() over (order by user_id) as num from User_Info) as  numTable
33 --where numTable.num between 4 and 6
34 ----over()开窗函数,ROW_NUMBER() ???
35 --select *,AVG(User_Age) over() as Age from User_Info
36 
37 --select * from 
38 --(select * ,Row_Number() over (order by user_Id) as num from User_Info) as numTable
39 --where numTable.num between 8 and 10
原文地址:https://www.cnblogs.com/NBOWeb/p/12374394.html