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日志重复输出问题

    出处:https://www.cnblogs.com/huang-yc/p/9209096.html 问题起源: ​ 在学习了python的函数式编程后,又接触到了logging这样一个强大的日志模块 ...

  2. springmvc4.3.7中使用RequestBody,传入json参数时,得到错误415 Unsupported Media Type

    在新建一个maven的项目的时候,当时并非springboot项目,是通过xml来配置的项目.在项目中DispatcherServlet的配置文件中配置了annotation-driven的, < ...

  3. [Linux系统] (2)用户权限管理

    示例---普通用户之间的文件共享:假设公司有2个项目组,共享同一台服务器. 1.为两个项目组各创建一个用户: useradd leo01 useradd leo02 2.为两个新用户设置密码: pas ...

  4. CSS 按钮

    总结有关按钮的各种样式 ㈠基本按钮样式 看一下没有进行css样式设计时按钮的样子与进行样式设计的按钮样子 <!DOCTYPE html> <html> <head> ...

  5. C# 动态访问webserver 帮助类

    /* 调用方式 * string url = "http://www.webservicex.net/globalweather.asmx" ; * string[] args = ...

  6. JAVA大文件上传断点续传解决方案

    javaweb上传文件 上传文件的jsp中的部分 上传文件同样可以使用form表单向后端发请求,也可以使用 ajax向后端发请求 1.通过form表单向后端发送请求 <form id=" ...

  7. 51 Nod 1134 最长递增子序列(经典问题回顾)

    1134 最长递增子序列  基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 给出长度为N的数组,找出这个数组的最长递增子序列.(递增子序列是指,子序列的元 ...

  8. 2-sat基础详解

    (大量引用<2-SAT解法浅析 -by 华中师大一附中 赵爽><由对称性解2-SAT问题> Great_Influence关于P4782 [模板]2-SAT 问题的题解.在此对 ...

  9. LocalDate/LocalDateTime与String的互相转换示例(附DateTimeFormatter详解)

    摘自:https://www.jianshu.com/p/b7e72e585a37 LocalDate/LocalDateTime与String的互相转换示例(附DateTimeFormatter详解 ...

  10. div随窗口变化设置高度

    window.onscroll = function () { sc(); }; window.onresize = function () { sc(); }; window.onload = fu ...