SQL左连接攻略--Mysql学习心得(符合sql标准)
2007-08-24 11:41
666 查看
SQL左外连接:明白左连接也就明白了右连接
两表外连接
出货表
客户id、客户名称、产品id、产品数量、备注
create table a_prod_out
(
custom_id int not null,
custom_name varchar(200) not null,
prod_id int default 0 null,
prod_num int default 0 null,
remark varchar(500) null,
constraint pk_a_prod_out primary key (custom_id)
)
产品表
产品id、产品名称、生产厂家
create table b_prod
(
prod_id int not null,
prod_name varchar(200) not null,
manufacturer_id int default 0 null,
remark varchar(500) null,
constraint pk_b_prod primary key (prod_id)
)
左连接
左连接以左表为标准,并接右边的部分数据,并接条件即产品id
此时左表信息全部显示,对应连接右边中符合并接条件的数据
select a.custom_id,a.custom_name,b.prod_name,b.manufacturer_id
from a_prod_out a left outer join b_prod b
on a.prod_id = b.prod_id ;
三表外连接
厂商表 客户对应用什么产品
create table a_mfter
(
manufacturer_id int not null,
manufacturer_name varchar(200) not null,
constraint pk_a_mfter primary key (manufacturer_id)
)
select a.custom_id,a.custom_name,b.prod_name,c.manufacturer_name
from a_prod_out a
left outer join b_prod b
on a.prod_id = b.prod_id
left outer join a_mfter c
on b.manufacturer_id = c.manufacturer_id;
SQL结果同样是以左表出货表为标准,并接产品表和厂商表
两表外连接
出货表
客户id、客户名称、产品id、产品数量、备注
create table a_prod_out
(
custom_id int not null,
custom_name varchar(200) not null,
prod_id int default 0 null,
prod_num int default 0 null,
remark varchar(500) null,
constraint pk_a_prod_out primary key (custom_id)
)
产品表
产品id、产品名称、生产厂家
create table b_prod
(
prod_id int not null,
prod_name varchar(200) not null,
manufacturer_id int default 0 null,
remark varchar(500) null,
constraint pk_b_prod primary key (prod_id)
)
左连接
左连接以左表为标准,并接右边的部分数据,并接条件即产品id
此时左表信息全部显示,对应连接右边中符合并接条件的数据
select a.custom_id,a.custom_name,b.prod_name,b.manufacturer_id
from a_prod_out a left outer join b_prod b
on a.prod_id = b.prod_id ;
三表外连接
厂商表 客户对应用什么产品
create table a_mfter
(
manufacturer_id int not null,
manufacturer_name varchar(200) not null,
constraint pk_a_mfter primary key (manufacturer_id)
)
select a.custom_id,a.custom_name,b.prod_name,c.manufacturer_name
from a_prod_out a
left outer join b_prod b
on a.prod_id = b.prod_id
left outer join a_mfter c
on b.manufacturer_id = c.manufacturer_id;
SQL结果同样是以左表出货表为标准,并接产品表和厂商表
相关文章推荐
- MySQL学习系列一---命令行连接mysql和执行sql文件
- MYSQL学习心得(1)--基础SQL语句
- sql必知会学习心得:mysql附带资源的导入
- php学习笔记(二)php与mysql连接与用php发送SQL查询
- MySQL学习笔记_12_Linux下C++/C连接MySQL数据库(二) --返回数据的SQL
- MYSQL学习心得(4) --SQL语句执行顺序
- mysql,SQL标准,多表查询中内连接,外连接,自然连接等详解之查询结果集的笛卡尔积的演化
- c# 连接 mysql 学习心得
- Spark学习(SparkSQL连接oralce,MySQL)
- 小白学习日记1:PL/SQL连接本地Oracle数据库Part2
- 树莓派学习笔记1 -- 解决mysql无法远程连接问题
- MySQL学习笔记9:连接查询
- MySql学习心得之存储过程
- 解决没有安装Qt 的mysql sql驱动插件情况下不能连接数据库的问题
- hibernate学习(一)hibernate.cfg.xml文件连接mySql、Oracle、SqlServer配置
- 使用powerdesigner连接数据库时出现Non SQL Error : Could not load class com.mysql.jdbc.Drive
- Qt5.4连接mysql出现QSqlDatabase: QMYSQL driver not loaded
- 深入学习之mysql(五)连接查询
- 学习笔记--mysql索引(二) B树索引的介绍和限制,好索引的标准
- [学习笔记]MySql之SQL语句优化