多表查询:标准SQL语法内连接总结

查看原文:http://www.sijitao.net/498.html
在前面的文章,章郎虫学习介绍了SQL的单表查询。因为单表查询很简单,相信朋友们一定都学会了。 接下来就总结下多表查询中应该注意的要点。

以下是内连接:

多表查询:标准SQL语法内连接总结
简单等连接select name,city
from salesreps join offices
on rep_office=office;
通过基于字段对间的精确匹配来匹配相关的字段,形成记录对。
显示等连接select name,city
from salesreps inner join offices
on rep_office=office;
使用关键字INNER JOIN来替代简单的JOIN的语法变化
父子查询select city,name,title
from offices join salesreps
on mgr=empl_num;
将一个表中的主键与另一个表中的相应外键匹配的等连接
记录选择规则select city,name,title
from offices join salesreps
on mgr=empl_num where target>600000;
通过添加一个WHERE谓词来从查询结果中过滤掉不想要的记录
多个匹配字段 select order_num,amount,description
from orders join products
on mfr=mfr_id and product=product_id;
多字段主键和外键要求多个记录与连接谓词匹配
三表连接 select order_num,amount,company,name
from orders join customers
on cust=cust_num
join salesreps on rep=empl_num
where amount>25000;
通过添加额外的JOIN字句来将多个表连接在一起
不等连接select name,quota,target
from salesreps join offices
on quota>target;
连接谓词中的比较运算符是非符号(=)的其他符号
自然连接 select order_num,amount,description
from orders natural join products;
基于所有在连接表间共享相同名称来匹配记录的等连接
使用USING子句连接 select order_num,amount,description from orders join products
using (mfr,product);
基于显示标识的字段名(在连接表中共享相同名称)的等连接
自连接select emps.name,mgrs.name
from salesreps emps join salesreps mgrs
on emps.manager=mgrs.empl_num;
一个表至其自身的等连接,其每条记录与同一个表中的其他记录匹配

 

我所使用的MySQL脚本:下载一下载二

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值