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——多表——外连接查询——左连接、右连接、复合条件查询的更多相关文章

  1. Oracle 左连接 left join、右连接right join说明

    Oracle 左.右连接 + 在等号 左边表示右连接  获取右表所有记录,即使左表没有对应匹配的记录. + 在等号 右边表示左连接  获取左表所有记录,即使右表没有对应匹配的记录. 例子: selec ...

  2. Linq和EF 做 单一条件查询 和 复合条件 查询 以及 多表 联合查询 示例

    单一条件查询: var table2Object = (from t1 in db.table1 join t2 in db.table2 on t1.id equals t2.id select t ...

  3. mysql概要(六)连接(内连接,左,右外连接

    内连接 [join on / from 表1,表二 ]效果一样 区别是:可以理解为首先取得笛卡儿积后,再匹配/还是根据条件获得笛卡尔积 内连接:取俩表的交叉匹配数据:(mysql 内连接 左连接 右连 ...

  4. 数据库左连接left join、右连接right join、内连接inner join on 及 where条件查询的区别

    join on 与 where 条件的执行先后顺序: join on 条件先执行,where条件后执行:join on的条件在连接表时过滤,而where则是在生成中间表后对临时表过滤 left joi ...

  5. 数据库中的左连接(left join)和右连接(right join)区别

    Left Join / Right Join /inner join相关 关于左连接和右连接总结性的一句话: 左连接where只影向右表,右连接where只影响左表. Left Join select ...

  6. 数据库~Mysql派生表注意的几点~关于百万数据的慢查询问题

    基础概念 派生表是从SELECT语句返回的虚拟表.派生表类似于临时表,但是在SELECT语句中使用派生表比临时表简单得多,因为它不需要创建临时表的步骤. 术语:*派生表*和子查询通常可互换使用.当SE ...

  7. 【hbase】——Java操作Hbase进行建表、删表以及对数据进行增删改查,条件查询

    1.搭建环境 新建JAVA项目,添加的包有: 有关Hadoop的hadoop-core-0.20.204.0.jar 有关Hbase的hbase-0.90.4.jar.hbase-0.90.4-tes ...

  8. (转)Java操作Hbase进行建表、删表以及对数据进行增删改查,条件查询

    1.搭建环境 新建JAVA项目,添加的包有: 有关Hadoop的hadoop-core-0.20.204.0.jar 有关Hbase的hbase-0.90.4.jar.hbase-0.90.4-tes ...

  9. Java操作Hbase进行建表、删表以及对数据进行增删改查,条件查询

    1.搭建环境 新建JAVA项目,添加的包有: 有关Hadoop的hadoop-core-0.20.204.0.jar 有关Hbase的hbase-0.90.4.jar.hbase-0.90.4-tes ...

随机推荐

  1. python字典的setdefault的妙用

    现在有一个员工字典,类似这样的结构 staff_dic = {"name":"灭霸", "age": 10000, "hobbie ...

  2. Acwing-271-杨老师的照相排列(DP)

    链接: https://www.acwing.com/problem/content/273/ 题意: 杨老师希望给他的班级拍一张合照. 学生们将站成左端对齐的多排,靠后的排站的人数不能少于靠前的排. ...

  3. 百度地图api,点击标注,改变标注marker图标的链接地址

    改变选中的图标样式 // 选中高亮标注图片 let mapIcon = ‘./icon.png’; //标注点 let markerArrs = [{},{},....]; // 点击标注点 mark ...

  4. python 手动拼接json数据

    第一步:分别拼接为字符串 第二步:将字符串转化为list 第三歩:将两个list合并为dict 第四步:将dict转换为接送数据 如:  import json keys = ['a', 'b', ' ...

  5. 10个PHP代码片段

    还记得CSDN研发频道此前发表过的一篇<可以直接拿来用的15个jQuery代码片段>吗?本文笔者将继续为你奉上10个超级有用的PHP代码片段. PHP是一种HTML内嵌式的语言,是一种在服 ...

  6. 拉格朗日插值法板子(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 ...

  7. The 10 Statistical Techniques Data Scientists Need to Master

    原文 就我个人所知有太多的软件工程师尝试转行到数据科学家而盲目地使用机器学习框架来处理数据,例如,TensorFlow或者Apache Spark,但是对于这些框架背后的统计理论没有完全的理解.所以提 ...

  8. 分布式-信息方式-JMS大纲

     一.简介 JMS即Java消息服务(Java Message Service)应用程序接口,是一个Java平台中关于面向消息中间件(MOM)的API,用于在两个应用程序之间,或分布式系统中发送消息, ...

  9. MySQL 获取格林时间

    SELECT *FROM 表面WHERE DATE_SUB( NOW( ), INTERVAL 3 MINUTE ) <= CONVERT_TZ( 时间字段, @@SESSION.time_zo ...

  10. AXIS2 通过 WSDL生成JAVA文件

    有时在我们的开发中可能会有这种情况就是你要使用webservice但是对方没有给你提供java文件,可能就只会给你一个wsdl文件,这种文件和xml文件是比较相似.axis2也给我们提供了很好的工具如 ...