首页 > 数据库技术 > 详细

MSSQL 4表联查,子查询,函数统计,分组条件查询综合应用实例

时间:2014-08-02 12:23:43      阅读:428      评论:0      收藏:0      [点我收藏+]
 1 SELECT 
 2  [UserName]
 3 ,[Yhxm]
 4 ,[Qx]
 5 ,[Phone]
 6 ,[Number] = COUNT([UserName])
 7 FROM 
 8 (SELECT 
 9      [UserName]   = A.[username] 
10     ,[Yhxm]          = B.[xm]          
11     ,[Qx]          = D.[P_Power]
12     ,[Phone]      = B.[dh] 
13     ,[DayCount]   = (SELECT COUNT(*) FROM [Caihuashun_FuJian_T] WHERE (SELECT (DATEDIFF(DAY,[F_bxsj],GETDATE())))   = 0) 
14     ,[MonthCount] = (SELECT COUNT(*) FROM [Caihuashun_FuJian_T] WHERE (SELECT (DATEDIFF(MONTH,[F_bxsj],GETDATE()))) = 0)  
15 FROM  
16 [Caihuashun_UserInfo_T] AS [A]
17 LEFT JOIN 
18 [Caihuashun_Vita_T] AS [B]
19 ON 
20 A.[username] = B.[username]
21 LEFT JOIN 
22 [Caihuashun_FuJian_T] AS [C]
23 ON 
24 C.[F_jxy] = A.[username]
25 LEFT JOIN 
26 [Caihuashun_Authority_T] AS [D]
27 ON 
28 D.[P_id] = A.[power]) 
29 AS [A]
30 GROUP BY [UserName],[Yhxm],[Qx],[Phone]

bubuko.com,布布扣

因为group by不能使用 where 所以我想查询UserName=452087948的那么只要在后面加上HAVING UserName =‘452087948‘ 那么查询的结果如下

bubuko.com,布布扣

MSSQL 4表联查,子查询,函数统计,分组条件查询综合应用实例,布布扣,bubuko.com

MSSQL 4表联查,子查询,函数统计,分组条件查询综合应用实例

原文:http://www.cnblogs.com/caihuashun/p/3886575.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!