查看各表之间的关系:关系图
视图:我们可以通过关系图工具来查看表间的关系,也可以利用视图工具来查看表间的关系(利用视图工具,查询定单表和客户表两个表中的数据),然后指出也可以通过查询分析器编写查询语句来实现多表的查询。
两个表的查询:
要
点:
两个表通过
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
�d�d�d
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、子查询:
查询中又嵌套一个查询�d�d
子查询本身也是一个完整的查询
那么什么样的查询可以作为子查询呢?
首先:
一个查询的返回值可以为:
单值:例如
select avg (unitprice) from products
�d�d�d
northwind
数据库
一列值:例如:
select city from authors
�d�d�d
pub
多列值(一个表):
select productname,unitprice,unitsinstock from products
�d�d
northwind
单值比较:子查询只能用
返回值为单值的查询语句
要
点:
返回值为单值的子查询
�d�d
northwind
数据库
select productname from products
where unitprice < (select avg (unitprice) from products)
avg()
聚合函数不能出现在
where
子句中,可以用子查询解决
范围比较或者查询:子查询可以用返回值为一列的查询语句
要
点:
返回值为一列值的子查询
�d�d
pubs
数据库
select pub_name from publishers
where city in ( select city from authors)
|