王利的博客
历史在哪里扭曲,就在哪里突破
posts - 17,  comments - 0,  trackbacks - 0
 

1.关键字

Top N 返回记录的条数,

top N percent  返回查出结果数量的百分比(四舍五入),

with ties order by,top 一起用能够查出和最后一条记录相等的数据,

distinct  消除重复记录

2.集合函数

Count(字段名) 统计数据忽略空行

count(*)  统计不忽略空行,avg,max,min,sum

3.分类汇总查询

Group by 子句按字段分类,相同字段为一组

注意:用于分类的字段必须是查询的字段,不要对包含多个空值的字段使用,空值也会作为一组

Having 子句只能在group by 子句中使用作为条件判断类似于where ,where中不能调用聚合函数,having中可以.

4.Rollup 操作符

Cube 操作符

Compute 子句

Compute by 子句

5.联接查询

内联接查询 : 返回满足联接条件的数据

1 inner join 2 on 联接表达式

:select distinct (lastname+' '+firstname) as name,orderid from employees as e inner join orders as o on e.employeeid=o.employeeid

外联接查询:返回满足联接条件的数据,也返回左边或者右边表不符合联接条件的记录

左向外联接:返回table1所有的记录,如果table1中的记录在table2中没有匹配的记录,则结果集和table2相关的字段为空值

Table1 left join table2 on 联接expression

Table1 left outer join table2 on 联接expression

右向外联接:返回table2所有的记录,如果table2中的记录在table1中没有匹配的记录,则结果集和table1相关的字段为空值

Table1 right join table2 on 联接expression

Table1 right outer table2 on 联接expression

完整外联接查询:返回两个表所有的记录,互相没有匹配记录时填充空值.

Table1 full join table2 on 联接expression

Table1 full outer table2 on 联接expression

 

交叉联接查询:如果没有where,返回table1,table2的笛卡儿乘积,既所有记录的不同交叉组合

Table1 cross join table2

 

6.合并多个记录集

Union  [all]操作符

要求引用的所有的表都有相似的数据结构,相同的字段数且每个查询中字段的顺序要相同

如果要全部返回不删除重复记录就使用all

尽量把复杂的查询分解提高查询效率.

 

7.子查询

一个查询语句嵌套在DML(select,update,delete,insert)语句中,则该语句被成为子查询.嵌套最多32,功能类似于联接查询,主要用于当查询需要多个步骤时.

分类:

相关子查询子查询重复执行,并将结果值代入外部查询的where子句进行评估.

内层子查询被反复执行,对外层查询的每行内层子查询都执行一次.

先外层查询再内层查询再外层再内层

:select lastname,firstname from employees as e where '1998-1-1' in (select orderdate from orders as o where o.employeeid=e.employeeid)

相关子查询的操作符 exists(not exists)用于限制外部查询,使其结果集符合子查询的条件,子查询返回true ,false

:select lastname,firstname from employees as e where exists (select * from orders as o where o.employeeid=e.e.employeeid and o.orderdate='1998-1-1')

嵌套子查询  只执行一次子查询并将结果值代入外部查询的where子句进行评估.

内层子查询执行完毕,再执行外层查询.

 :select lastname,firstname from employees where employeesid in (select employeesid from orders where orders.orderdate='1998-1-1')

 

子查询可以作为派生的表

可以作为表达式

Select  productname,unitprice,(unitprice-(select avg(unitprice) from products) ) as diff from products where productname='chai'

模拟联接子句

模拟having子句

posted @ 2006-10-09 14:21 疾风之狼的java之旅 阅读(276) | 评论 (0)编辑 收藏

<2006年10月>
24252627282930
1234567
891011121314
15161718192021
22232425262728
2930311234

常用链接

留言簿(1)

随笔分类

随笔档案

文章档案

相册

友情链接

搜索

  •  

积分与排名

  • 积分 - 4092
  • 排名 - 3258

最新评论

阅读排行榜

评论排行榜