星空网 > 软件开发 > 数据库

多表数据连接 Left join

一个我写的实例:其中多表连接,一共连接了3个表。使用聚集函数SUM,用到了GROUP BY


多表数据连接 Left joinSELECT a.[UserID],b.[Name],sum (c.[Money]+c.[Bank])as TotalMoney
多表数据连接 Left joinFROM Table1 a(nolock)
多表数据连接 Left join LEFT JOIN Table2 b(nolock) on a.[UserID] = b.[UserID] 
多表数据连接 Left joinLEFT JOIN Table3 c(nolock) ON b.[UserID]=c.[UserID] 
多表数据连接 Left join
多表数据连接 Left joinWHERE  a.[UserID] = b.[UserID] and a.[UserID] = c.[UserID] and a.[Time] >= '2005-01-01' AND a.[Time] <= '2006-12-31' 
多表数据连接 Left join
多表数据连接 Left joinGROUP BY a.[UserID],b.[Name]
多表数据连接 Left join
多表数据连接 Left joinORDER BY a.[Time] DESC 

优化一下
多表数据连接 Left joinSELECT a.[UserID],b.[Name],sum (c.[Money]+c.[Bank])as TotalMoney
多表数据连接 Left joinFROM Table1 a(nolock)
多表数据连接 Left joinLEFT JOIN Table3 c(nolock) ON a.[UserID]=c.[UserID],  Table2 b(nolock) 
多表数据连接 Left join
多表数据连接 Left joinWHERE  a.[UserID] = b.[UserID] and a.[Time] >= '2005-01-01' AND a.[Time] <= '2006-12-31' 
多表数据连接 Left join
多表数据连接 Left joinGROUP BY a.[UserID],b.[Name]
多表数据连接 Left join
多表数据连接 Left joinORDER BY a.[Time] DESC 

================================================================================
Left Join 语法:
多表数据连接 Left joinselect   *   from
多表数据连接 Left jointable1   left   join   table2   on   条件1   
多表数据连接 Left joinleft   join   table3   on   条件2   
多表数据连接 Left joinleft   join   table4   on   条件3  
多表数据连接 Left joinwhere   条件4

GROUP BY 说明:

多表数据连接 Left joingroup by 
多表数据连接 Left join
多表数据连接 Left join    在select 语句中可以使用group by 子句将行划分成较小的组,然后,使用聚组函数返回每一个组的汇总信息,另外,可以使用having子句限制返回的结果集。group by 子句可以将查询结果分组,并返回行的汇总信息Oracle 按照group by 子句中指定的表达式的值分组查询结果。
多表数据连接 Left join
多表数据连接 Left join   在带有group by 子句的查询语句中,在select 列表中指定的列要么是group by 子句中指定的列,要么包含聚组函数
多表数据连接 Left join
多表数据连接 Left join   select max(sal),job emp group by job;
多表数据连接 Left join   (注意max(sal),job的job并非一定要出现,但有意义)
多表数据连接 Left join
多表数据连接 Left join   查询语句的select 和group by ,having 子句是聚组函数唯一出现的地方,在where 子句中不能使用聚组函数。
多表数据连接 Left join
多表数据连接 Left join  select deptno,sum(sal) from emp where sal>1200 group by deptno having sum(sal)>8500 order by deptno;
多表数据连接 Left join
多表数据连接 Left join  当在gropu by 子句中使用having 子句时,查询结果中只返回满足having条件的组。在一个sql语句中可以有where子句和having子句。having 与where 子句类似,均用于设置限定条件
多表数据连接 Left join 
多表数据连接 Left join  where 子句的作用是在对查询结果进行分组前,将不符合where条件的行去掉,即在分组之前过滤数据,条件中不能包含聚组函数,使用where条件显示特定的行。
多表数据连接 Left join  having 子句的作用是筛选满足条件的组,即在分组之后过滤数据,条件中经常包含聚组函数,使用having 条件显示特定的组,也可以使用多个分组标准进行分组。
多表数据连接 Left join
多表数据连接 Left join  查询每个部门的每种职位的雇员数
多表数据连接 Left join  select deptno,job,count(*) from emp group by deptno,job;
多表数据连接 Left join




原标题:多表数据连接 Left join

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流