<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

 
查看各表之间的关系:关系图

视图:我们可以通过关系图工具来查看表间的关系,也可以利用视图工具来查看表间的关系(利用视图工具,查询定单表和客户表两个表中的数据),然后指出也可以通过查询分析器编写查询语句来实现多表的查询。

 

两个表的查询:

   点:
两个表通过where关系中的相同的字段值连接。 用于连接的字段名在两个表中可以不同,但是必须是代表相同的含义
    
注意那个[ ] 因为order details有空格,   “ . “ 用于连接表和字段
select [order details].orderid,products.productname,

[order details].quantity,products.unitsinstock

from [order details] ,products

where [order details].productid = products.productid

 

三个表的查询:where只是定义筛选条件,可以有很多筛选条件and/or连接
下面:列出select from where and的主干,可以提示公共字段,让学员来完成where子句)

  点:
三个表通过where关系中的相同的字段值连接。 用于连接的字段名在两个表中可以不同,但是必须是代表相同的含义
[含义]每笔订单与雇员和客户的关系
select orderid,lastname,contactname

from orders,employees,customers

where orders.employeeid=employees.employeeid

and orders.customerid=customers.customerid

 

     3、使用表的别名:

from中的表可以是外部的连接表,当使用完整名称(server.db.schema.table)时,会很繁琐。可以使用as 来设置别名会比较简单,看起来比较清晰。加as的目的就是使代码清晰。

 

使用表的别名:

  点: from子句的表名后加上“as别名”,以后的子句中直接引用别名
[含义]从订单表order details和产品表products中得到订单编号orderid、产品名称productname、订单数量quantity和产品库存量unitsinstock

select od.orderid,p.productname,

od.quantity,p.unitsinstock

from [order details] as od ,products as p

where od.productid = p.productid

 

三个表的别名:

[含义]改写三个表的查询例句,每笔订单与雇员和客户的关系
select orderid,lastname,contactname

from orders,employees,customers

where orders.employeeid= employees.employeeid

and orders.customerid= customers.customerid

 

     4、使用union来合并结果集:

实质:是多个select 查询语句,将他们的查询结果连续输出,
各个select查询语句各自独立完成,只是结果连接起来一起输出
要求: 每个select返回的列数和列的顺序必须一致.
每列的数据类型必须一一对应且兼容(可以不同, 例如 l 兼容 int
union all 包含所有重复列        union 不包含重复列
 

union 连接表:

  点: union连接两个(或者多个)select 查询,每个查询独自执行,注意union
能够实现的要求。
[含义]:查询所有雇员和客户的所在城市和邮政编码
select companyname, c contacttitle          ╠╠╠ northwind 数据库

       from customers

union

select companyname, c contacttitle

       from suppliers

 

内连接:语法就是在from子句中使用inner join把两个或者两个以上的表联接起来,用关键字on来指定联接条件

[含义]:从订单表order details和产品表products中得到订单编号orderid、产品名称productname、订单数量quantity和产品库存量unitsinstock

select od.orderid,p.productname,

             od.quantity,p.unitsinstock

from [order details] as od inner join products as p

on od.productid = p.productid

特点: 只返回两个表中在连接列具有相等值的列  
如果有不相等值的列,不包括在结果集中。不等连接极少使用,了解即可
 

外连接:会返回from子句中提到的至少一个表或视图的所有行

select a.au_fname, a.au_lname, p.pub_name

from authors a left outer join publishers p

   on a.city = p.city

order by p.pub_name asc, a.au_lname asc, a.au_fname asc

演示前,分析两张表, 演示后,分析结果
特点:
left outer join  就是前表 inner
left outer join left join 类似的 right full
right outer join 就是 后表 inner   颠倒两个表的顺序,就是left outer join
full join 或者 full outer join   前表 后表
 

交叉连接:一般用于测试数据库的性能

use northwind

select employeeid,customerid

from employees cross join  customers

order by employeeid

 

自连接:表内的信息的连接 可以同一字段,也可以不同字段
select e.lastname as 员工的名字,m.lastname as 上司的名字

from employees as e left join employees as m

on e.reportsto = m.employeeid
left join join的区别  查看结果分析
问题 如果要做排序,例如: 以“上司的名字”来排序, 应该在 order by 子句中使用什么字段名?
答案: m.lastname “上司的名字” 而不能用lastname
 

     5、子查询:

查询中又嵌套一个查询╠╠ 子查询本身也是一个完整的查询
那么什么样的查询可以作为子查询呢?
首先: 一个查询的返回值可以为:
单值:例如select avg (unitprice) from products   ╠╠╠ northwind 数据库
一列值:例如: select city from authors           ╠╠╠ pub
多列值(一个表):select productname,unitprice,unitsinstock from products╠╠northwind
 

单值比较:子查询只能用 返回值为单值的查询语句
  点: 返回值为单值的子查询     ╠╠ northwind 数据库
select productname from products
where unitprice < (select avg (unitprice) from products)
avg() 聚合函数不能出现在where子句中,可以用子查询解决
 

范围比较或者查询:子查询可以用返回值为一列的查询语句
  点: 返回值为一列值的子查询     ╠╠ pubs 数据库
select pub_name from publishers
where city in ( select city from authors)