mysql——多表——外连接查询——左连接、右连接、复合条件查询
create table employee ( num int(50),
d_id int(50),
name varchar(50),
age int(50),
sex varchar(50),
homeadd varchar(50)
); insert into employee values(1,1001,'zhangsan',26,'nan','beijing');
insert into employee values(2,1001,'lisi',24,'nv','hunan');
insert into employee values(3,1002,'wangwu',25,'nan','jiangsu');
insert into employee values(4,1004,'aric',15,'nan','yingguo'); select * from employee; create table department ( d_id int(50),
d_name varchar(50),
functione varchar(50),
address varchar(50)
); insert into department values(1001,'keyanbu','yanfachanpin','3lou5hao');
insert into department values(1002,'shengchanbu','shengchanchanp','5louyiceng');
insert into department values(1003,'xiaoshoubu','cehuaxiaoshou','1louxiaoshoudating'); select * from department; select num,name,employee.d_id,age,sex,d_name,functione from employee,department where employee.d_id = department.d_id; 内连接查询:可以查询两个或者两个以上的表,当两个表中存在表示相同意义的字段时,可以通过该字段来连接这两个表; 当该字段的值相等时,就查询出该记录。 ====================================================================================================================== 外连接查询:可以查询两个或者两个以上的表,外连接查询也需要通过指定字段来进行连。 当该字段取值相等时,可以查询出该记。 而且该字段取值不相等的记录也可以查询出来。 外连接包括:左连接、右连接 语法: select 属性列表 from 表名1 left | right join 表名2 on 表名1.属性名1 = 表名2.属性名2; 属性列表表示要查询的字段的名称,这些字段可以来自不同的表; Left表示左连接查询; rigth表示右连接查询; on后面接的是连接的条件; 1、左连接查询 进行左连接查询时,可以查询出表名1所指的表中的所有记录。而表名2所指的表中,只能查询出匹配的记录 select * from employee;
select * from department; select num,name,employee.d_id,age,sex,d_name,functione from employee left join department on employee.d_id = department.d_id; 2、右连接查询 进行右连接查询时,可以查询出表名2所指的表中的所有记录。而表名1所指的表中,只能查询出匹配的记录 select * from employee;
select * from department; select num,name,employee.d_id,age,sex,d_name,functione from employee right join department on employee.d_id = department.d_id; 复合条件查询 在连接查询时,通过增加其他的限制条件,可以使查询结果更加准确 select * from employee;
select * from department; select num,name,employee.d_id,age,sex,d_name,functione from employee,department where employee.d_id = department.d_id; select num,name,employee.d_id,age,sex,d_name,functione from employee,department where employee.d_id = department.d_id and age > 24; select num,name,employee.d_id,age,sex,d_name,functione from employee,department where employee.d_id = department.d_id order by age asc; select 语句先按照内连接的方式从employee和department表中查询出数据。然后查询结果按照age字段从小到大的顺序进行排序。 =================================================================================================================================
前期准备表语句:
create table employee ( num int(50),
d_id int(50),
name varchar(50),
age int(50),
sex varchar(50),
homeadd varchar(50)
); insert into employee values(1,1001,'zhangsan',26,'nan','beijing');
insert into employee values(2,1001,'lisi',24,'nv','hunan');
insert into employee values(3,1002,'wangwu',25,'nan','jiangsu');
insert into employee values(4,1004,'aric',15,'nan','yingguo'); select * from employee; create table department ( d_id int(50),
d_name varchar(50),
functione varchar(50),
address varchar(50)
); insert into department values(1001,'keyanbu','yanfachanpin','3lou5hao');
insert into department values(1002,'shengchanbu','shengchanchanp','5louyiceng');
insert into department values(1003,'xiaoshoubu','cehuaxiaoshou','1louxiaoshoudating'); select * from department;
select * from employee;

select * from department;

左连接查询:
语法:
select 属性列表 from 表名1 left | right join 表名2 on 表名1.属性名1 = 表名2.属性名2;
属性列表表示要查询的字段的名称,这些字段可以来自不同的表;
Left表示左连接查询;
rigth表示右连接查询;
on后面接的是连接的条件;
1、左连接查询
进行左连接查询时,可以查询出表名1所指的表中的所有记录。而表名2所指的表中,只能查询出匹配的记录
select num,name,employee.d_id,age,sex,d_name,functione from employee left join department on employee.d_id = department.d_id;

2、右连接查询
进行右连接查询时,可以查询出表名2所指的表中的所有记录。而表名1所指的表中,只能查询出匹配的记录
select num,name,employee.d_id,age,sex,d_name,functione from employee right join department on employee.d_id = department.d_id;

3、复合条件查询
在连接查询时,通过增加其他的限制条件,可以使查询结果更加准确
select num,name,employee.d_id,age,sex,d_name,functione from employee,department where employee.d_id = department.d_id; select num,name,employee.d_id,age,sex,d_name,functione from employee,department where employee.d_id = department.d_id and age > 24; select num,name,employee.d_id,age,sex,d_name,functione from employee,department where employee.d_id = department.d_id order by age asc;
mysql——多表——外连接查询——左连接、右连接、复合条件查询的更多相关文章
- Oracle 左连接 left join、右连接right join说明
Oracle 左.右连接 + 在等号 左边表示右连接 获取右表所有记录,即使左表没有对应匹配的记录. + 在等号 右边表示左连接 获取左表所有记录,即使右表没有对应匹配的记录. 例子: selec ...
- Linq和EF 做 单一条件查询 和 复合条件 查询 以及 多表 联合查询 示例
单一条件查询: var table2Object = (from t1 in db.table1 join t2 in db.table2 on t1.id equals t2.id select t ...
- mysql概要(六)连接(内连接,左,右外连接
内连接 [join on / from 表1,表二 ]效果一样 区别是:可以理解为首先取得笛卡儿积后,再匹配/还是根据条件获得笛卡尔积 内连接:取俩表的交叉匹配数据:(mysql 内连接 左连接 右连 ...
- 数据库左连接left join、右连接right join、内连接inner join on 及 where条件查询的区别
join on 与 where 条件的执行先后顺序: join on 条件先执行,where条件后执行:join on的条件在连接表时过滤,而where则是在生成中间表后对临时表过滤 left joi ...
- 数据库中的左连接(left join)和右连接(right join)区别
Left Join / Right Join /inner join相关 关于左连接和右连接总结性的一句话: 左连接where只影向右表,右连接where只影响左表. Left Join select ...
- 数据库~Mysql派生表注意的几点~关于百万数据的慢查询问题
基础概念 派生表是从SELECT语句返回的虚拟表.派生表类似于临时表,但是在SELECT语句中使用派生表比临时表简单得多,因为它不需要创建临时表的步骤. 术语:*派生表*和子查询通常可互换使用.当SE ...
- 【hbase】——Java操作Hbase进行建表、删表以及对数据进行增删改查,条件查询
1.搭建环境 新建JAVA项目,添加的包有: 有关Hadoop的hadoop-core-0.20.204.0.jar 有关Hbase的hbase-0.90.4.jar.hbase-0.90.4-tes ...
- (转)Java操作Hbase进行建表、删表以及对数据进行增删改查,条件查询
1.搭建环境 新建JAVA项目,添加的包有: 有关Hadoop的hadoop-core-0.20.204.0.jar 有关Hbase的hbase-0.90.4.jar.hbase-0.90.4-tes ...
- Java操作Hbase进行建表、删表以及对数据进行增删改查,条件查询
1.搭建环境 新建JAVA项目,添加的包有: 有关Hadoop的hadoop-core-0.20.204.0.jar 有关Hbase的hbase-0.90.4.jar.hbase-0.90.4-tes ...
随机推荐
- Acwing-97-约数之和(整数分解, 递推分治)
链接: https://www.acwing.com/problem/content/99/ 题意: 假设现在有两个自然数A和B,S是AB的所有约数之和. 请你求出S mod 9901的值是多少. 思 ...
- eclipse svn同步过滤掉某些不需要同步的文件
注:这里说的svn是eclipse里svn插件 默认情况下,我们在点击svn同步时,总是会把一些不需要的目录和文件也给同步了,这样我觉得很晃眼睛,所以在这里说下怎么去去掉不想同步的文件 1.默认同步下 ...
- http学习--常用请求方法和响应状态码
常用的http请求方法: GET方法:请求服务器资源,并返回 POST方法:向指定资源提交数据进行处理请求(比如说表单,上传文件等).数据被包含在请求体中.POST请求可能会导致新的资源建立或已有资源 ...
- linux运维、架构之路-MongoDB单机部署
一.MongoDB介绍 MongoDB 是一个基于分布式文件存储的数据库.由 C++ 语言编写.旨在为 WEB 应用提供可扩展的高性能数据存储解决方案. MongoDB 是一个介于关系型数据库和非关系 ...
- Eclipse快捷键和IDEA对比
- JDK、JRE和JVM的区别与相互之间的联系
工作这么久,好多时间都在研究一些并发.多线程.分布式.框架等这些东西,但是前几天突然被人问到jdk.jre.jvm的区别与联系,瞬间觉得一脸懵逼,感觉还是有必要重新整理一下一些比较基本的java知识了 ...
- Redis总结 C#中如何使用redis
转载自:https://www.cnblogs.com/zhangweizhong/p/4972348.html 本篇着重讲解.NET中如何使用redis和C#. Redis官网提供了很多开源的C#客 ...
- 微信小程序_(map)简单的小地图
map地图效果 官方文档:传送门 Page({ data: { markers: [{ iconPath: "/resources/others.png", id: 0, lati ...
- 拉格朗日插值法板子(dls)
namespace polysum { ; ll a[D],f[D],g[D],p[D],p1[D],p2[D],b[D],h[D][],C[D]; ll calcn(int d,ll *a,ll n ...
- Android学习_7/23
1. 在活动中使用Menu 1) 什么是Menu? 2) 怎么实现? step1:res目录下创建Menu resource file,使用<item… ...