表一
表二
1.Select
select all 性别 from people --将返回27个值,虽然内容都是"男"和"女" select distinct 性别 from people --只会显示两个值"男"和"女" select top 5 性别 from people --只显示前5条检索到的信息 select top 30 percent 性别 from people --显示前30%检索到的信息 |
2.Into
--将people表中的姓名列检索出,并放在tempTable里,如果不存在tempTable表,则新建 |
3.From
--我们要看看不同的统率力下的带兵数量,而统率和士兵并不在同一个表中 select PersonPower.统率,people.士兵 |
4.Where
select 武将 from PersonPower where 武力!>90 --查询武力不大于90的武将 --查询条件为智力不在70--100之间 --查询条件是否为空值 --查询是否为枚举中的值 --查询姓氏为"黄"的两个字名字,这里结果为'黄忠'和'黄盖' --查询姓氏为"黄"的所有名字,这里结果为'黄忠'、'黄盖'、'黄月瑛' --查询以H-Z字母开头的记录,这里以pubs数据库为例,结果为Thomas --查询不以字母T开头的记录 --假如数据中本身带有通配符,如:My_Name@163.com,则使用逃逸字符'Escape'来说明 |
5.Group by
--统计各个阵营的带兵总数 select 阵营,性别,sum(士兵) as 士兵总数 from People group by 性别,阵营
--Cube的用法,显示所有的组合 select 阵营,性别,sum(士兵) as 士兵总数 from People group by 阵营,性别 with cube
--Rollup的用法,只匹配Group by第一分组条件 select 阵营,性别,sum(士兵) as 士兵总数 from People group by 阵营,性别 with rollup
|
6.Having
--Having子句作用于组,而Where子句作用于表和视图,Having主要用来对计算的结果进一步筛选 --这个将最后结果筛选掉了只有一个女将的魏国 |
7.Compute 与 Compute by
--Compute将在最后添加一行表示计算结果的数据,而Compute by与Group by功能类似,也是用于计算,但Compute by能产生多个结果集 select 姓名,士兵 from People Compute sum(士兵) --最后有一行sum为1074790 --按阵营分成三组结果集,然后每组结果集最后都有一行总和值 |
8.Union
--Union用于联合多个查询结果集,且默认为删除重复记录,如要显示全部,加上all --假设我们要得到多个表的所有姓名 select 姓名 from People union --这里如果加上all的话,将显示出相同记录 select 武将 from PersonPower |
9.Join On
--Join用于连接两个不同的表,On用于给出这两个表之间的连接条件 insert into People values(28,'胡晓伟','男','中国',88888) --People中插入一条新记录 --内联接:默认联接形式,返回两个表中所有匹配的行 --左向外联接:匹配join左边的表,如果在右边并没有相应记录,则置为NULL --右向外联接:同上,匹配join右边的表 --完整外联接:同时匹配两个表 --交叉连接:返回两个表中记录的笛卡儿乘积,这里On关键字不能用 |
10.嵌套查询
--单值比较 select 武将 from PersonPower --In --查询魏国武将的属性值 --All --查询统率力最小的武将 --Some(任意一个值) --查询统率力不是最小的武将(最小的那个记录不满足>关系,其它的至少能比一个值大) --Exists(表之间的关系,不必对应到具体列,这是和In的最大区别) --查询蜀国武将的姓名及武力 --Not Exists --查询非蜀国武将的姓名及武力 |