Mysql---查询进阶(二)
内连接之非等值连接: 使用非相等做判断做连接条件
环境准备:创建工资等级表
mysql>use tarena;
mysql> create table wage_grade(
id int primary key auto_increment, grade char(1), low int , high int ); mysql>insert into wage_grade(grade,low,high) values('A', 5000, 8000),('B', 8001, 10000),('C', 10001, 15000),('D', 15001, 20000),
('E', 20001, 1000000);mysql> select * from wage_grade;
+----+-------+-------+---------+
| id | grade | low | high |
+----+-------+-------+---------+
| 1 | A | 5000 | 8000 |
| 2 | B | 8001 | 10000 |
| 3 | C | 10001 | 15000 |
| 4 | D | 15001 | 20000 |
| 5 | E | 20001 | 1000000 |
+----+-------+-------+---------+
5 rows in set (0.00 sec)
查询2018年12月员工基本工资级别
select employee_id, date, basic, grade from salary as s inner join wage_grade as g on s.basic between g.low and g.high where year(date)=2018 and month(date)=12;
查询2018年12月员工各基本工资级别的人数
select grade, count(employee_id) as numbersfrom salary as s inner join wage_grade as g on s.basic between g.low and g.highwhere year(date)=2018 and month(date)=12 group by grade;
查询2018年12月员工基本工资级别,员工需要显示姓名
select name, date, basic, grade from employees as e inner join salary as s on e.employee_id=s.employee_id inner join wage_grade as g on s.basic between g.low and g.high where year(date)=2018 and month(date)=12;
给表头定义别名
mysql> select name as 姓名, date as 发工资日期, basic as 基本工资, grade as 工资等级 from employees as e inner join salary as s on e.employee_id=s.employee_id inner join wage_grade as g on s.basic between g.low and g.high where year(date)=2018 and month(date)=12 ;
内连接之自连接:
自己连接自己 把1张当2张表使用,实现的方法
就是查询表的时候给表定义别名来实现。
查看哪些员工的生日月份与入职月份相同
select e.name, e.hire_date, em.birth_date
from employees as e inner join employees as em
on month(e.hire_date)=month(em.birth_date) and e.employee_id=em.employee_id;
或 where 条件
mysql> select name ,hire_date , birth_date from employees -> where month(hire_date) = month(birth_date);
外连接分类如下:
左外连接 LEFT JOIN
左边表的记录全都显示出来 右边的表只显示与条件匹配记录,右边表比左边表少的记录使用NULL匹配
右外连接 RIGHT JOIN
右边表的记录全都显示出来 左边的表只显示与条件匹配记录,左表比右边表少的记录使用NULL 匹配
全外连接(mysql不支持,可以使用UNION实现相同的效果) :合并查询结果
外连接的应用场景:
1)比较2个表里记录的不同
2)者哪些数据当前表有而另一张表没有。
环境准备:
向departments表里添加3个部门 行政部 小卖部 公关部
mysql> insert into departments(dept_name) values("小卖部"),("行政部"),("公关部");
mysql> select name,dept_id from employees where name in ("bob","tom","lily");
+------+---------+
| name | dept_id |
+------+---------+
| bob | NULL |
| tom | NULL |
| lily | NULL |
+------+---------+
3 rows in set (0.00 sec)
mysql>
向employees表中添加3个员工 只给name字段赋值
mysql> insert into employees(name)values("bob"),("tom"),("lily");
mysql> select * from tarena.departments;
+---------+-----------+
| dept_id | dept_name |
+---------+-----------+
| 1 | 人事部 |
| 2 | 财务部 |
| 3 | 运维部 |
| 4 | 开发部 |
| 5 | 测试部 |
| 6 | 市场部 |
| 7 | 销售部 |
| 8 | 法务部 |
| 9 | 小卖部 |
| 10 | 行政部 |
| 11 | 公关部 |
+---------+-----------+
11 rows in set (0.00 sec)
mysql>
查看哪些员工没有部门
mysql> select e.name,d.dept_name from departments as d right join employees as e on d.dept_id=e.dept_id;
...
...
| 刘倩 | 法务部 |
| 杨金凤 | 法务部 |
| bob | NULL |
| tom | NULL |
| lily | NULL |
+-----------+-----------+
136 rows in set (0.00 sec)mysql> select e.name,d.dept_name from departments as d right join employees as e on d.dept_id=e.dept_id
where d.dept_name is null ;
+------+-----------+
| name | dept_name |
+------+-----------+
| bob | NULL |
| tom | NULL |
| lily | NULL |
+------+-----------+
3 rows in set (0.00 sec)
查看哪些部门没有员工
mysql> select d.dept_name,e.name from departments as d left join employees as e on d.dept_id=e.dept_id;
...
...
| 法务部 | 王荣 |
| 法务部 | 刘倩 |
| 法务部 | 杨金凤 |
| 小卖部 | NULL |
| 行政部 | NULL |
| 公关部 | NULL |
+-----------+-----------+
136 rows in set (0.00 sec)mysql> select d.dept_name,e.name from departments as d left join employees as e on d.dept_id=e.dept_id where e.name is null;
+-----------+------+
| dept_name | name |
+-----------+------+
| 小卖部 | NULL |
| 行政部 | NULL |
| 公关部 | NULL |
+-----------+------+
3 rows in set (0.01 sec)mysql>
联合查询
也称联合查询,用来合并查询结果
可以合并同一张的表的查询记录(不同表的查询记录也可合并)
要求查询时,多个select语句的检索到的字段数量必须一致
每一条记录的各字段类型和顺序最好是一致的
UNION关键字默认去重,可以使用UNION ALL包含重复项
语法格式 1 (SELECT语句 ) UNION (SELECT语句);
语法格式 2 (SELECT语句 ) UNION ALL (SELECT语句);
语法格式演示:
mysql> select "abc";
+-----+
| abc |
+-----+
| abc |
+-----+
1 row in set (0.00 sec)mysql> select "bcd";
+-----+
| bcd |
+-----+
| bcd |
+-----+
1 row in set (0.00 sec)mysql> (select "bcd") union (select "xyz");
+-----+
| bcd |
+-----+
| bcd |
| xyz |
+-----+
2 rows in set (0.00 sec)mysql>
mysql> (select "xyz") union (select "xyz");
+-----+
| xyz |
+-----+
| xyz |
+-----+
1 row in set (0.00 sec)mysql> (select "xyz") union all (select "xyz");
+-----+
| xyz |
+-----+
| xyz |
| xyz |
+-----+
2 rows in set (0.00 sec)mysql>
查询 1972年 或 2000年后 出生的员工
使用where 条件查询
select name, birth_date from employees
where year(birth_date)=1972 or year(birth_date)>2000;
或 使用联合查询
( select name, birth_date from employees where year(birth_date)=1972)
union
(select name, birth_date from employees where year(birth_date)>2000 );
一起输出user表中uid号最小和uid号最大的用户名和uid号
select min(uid) from user;
select max(uid) from user;(select name, uid from user where uid = (select min(uid) from user))
union
(select name, uid from user where uid = (select max(uid) from user));
嵌套查询
查询运维部所有员工信息(部门名称是运维部的员工信息)
#先把 运维部的id 找到select dept_id from departments where dept_name="运维部";#员工表里没有部门名称 但有部门编号 (和部门表的编号是一致的)select * from employees where dept_id = (select dept_id from departments where dept_name="运维部");
查询2018年12月所有比100号员工基本工资高的工资信息
#把100号员工的基本工资查出来
select basic from salary where year(date)=2018 and
month(date)=12 and employee_id=100; #查看比100号员工工资高的
select * from salary
where year(date)=2018 and month(date)=12 and
basic>(select basic from salary where year(date)=2018 and
month(date)=12 and employee_id=100);
查询部门员工人数 比 开发部人数少 的 部门
#统计每个部门的人数 select dept_id , count(name) from employees group by dept_id ;#统计开发部 员工人数mysql> select count(*) from employees where dept_id = (select dept_id from departments where dept_name="开发部");#输出人数比开发部少的 部门 及 人数
select dept_id , count(name) as 部门人数 from employees group by dept_id
having count(name)<(
select count(name) from employees where dept_id=(
select dept_id from departments where dept_name='开发部')
);
查询每个部门的人数
#显示部门表中的所有列表select d.* from departments as d; #查询每个部门的人数
select d.* , ( select count(name) from employees as e where d.dept_id=e.dept_id) as 总人数
from departments as d;
查询人事部和财务部员工信息
#查看人事部和财务部的 部门idselect dept_id from departments where dept_name in ('人事部', '财务部');#查询人事部和财务部员工信息
select dept_id , name from employees
where dept_id in (
select dept_id from departments where dept_name in ('人事部', '财务部') );
查询人事部2018年12月所有员工工资
select * from salary where year(date)=2018 and month(date)=12
and employee_id in (select employee_id from employees
where dept_id=(select dept_id from departments where dept_name='人事部') );
查找2018年12月基本工资和奖金都是最高的工资信息
select * from salary
where year(date)=2018 and month(date)=12 and
basic=(select max(basic) from salary where year(date)=2018 and month(date)=12 )
and
bonus=(select max(bonus) from salary where year(date)=2018 and month(date)=12);
查询3号部门及其部门内 员工的编号、名字 和 email
select dept_id, dept_name, employee_id, name, email from (
select d.dept_name, e.* from departments as d inner join employees as e
on d.dept_id=e.dept_id ) as tmp_table where dept_id=3;
多表更新与删除
一起修改或删除多张表里的数据
环境准备:
mysql> use tarena;
mysql> create table t3 select name , uid from tarena.user limit 2;
mysql> create table t4 select uid,homedir,shell from tarena.user limit 4;
mysql> select * from t3 ;
mysql> select * from t4;
uid 字段 是 t3 和 t4 表的 关联字段
select * from t3 inner join t4 on t3.uid = t4.uid ;#多表修改update t3 inner join t4 on t3.uid = t4.uid set t3.uid=101 , t4.uid=102 where t3.uid=0 ;#查看修改select * from t3 ; 原先UID=0 没了 看到是 UID = 101select * from t4; 原先UID=0 没了 看到 UID = 102
#删除时使用的多表连接命令
mysql> select * from t3 inner join t4 on t3.uid = t4.uid;
+------+------+------+---------+---------------+
| name | uid | uid | homedir | shell |
+------+------+------+---------+---------------+
| bin | 1 | 1 | /bin | /sbin/nologin |
+------+------+------+---------+---------------+
1 row in set (0.00 sec)
#多表删除
mysql> delete t3,t4 from t3 inner join t4 on t3.uid = t4.uid;
Query OK, 2 rows affected (0.06 sec)mysql> select * from t3;
+------+------+
| name | uid |
+------+------+
| root | 101 |
+------+------+
1 row in set (0.00 sec)mysql> select * from t4;
+------+----------+---------------+
| uid | homedir | shell |
+------+----------+---------------+
| 102 | /root | /bin/bash |
| 2 | /sbin | /sbin/nologin |
| 3 | /var/adm | /sbin/nologin |
+------+----------+---------------+
3 rows in set (0.00 sec)mysql>
Mysql---查询进阶(二)相关推荐
- MySQL查询进阶之多表查询
一.多表查询 1.引出 2.笛卡尔积 3. 笛卡尔积的解决方法 二.多表查询分类 1.等值连接和非等值连接 2.自连接和非自连接 3.内连接和外连接 SQL92:使用(+)创建连接 SQL99语法实现 ...
- MySQL Ⅳ 查询进阶
MySQL 聚合函数 count sum avg max min group by having 联合查询 内连接 外连接 自连接 子查询 单行子查询 多行子查询 (not) in (not) exi ...
- mysql查询进阶——员工表与部门表连接查询
如下是员工表与部门表的表内容. 查询月薪最高的员工姓名和月薪(子查询) select ename, sal from tb_emp where sal = (select max(sal) from ...
- mysql可以存储标点么_MySQL查询(进阶)(每个标点都是重点)
MySQL 是工作中很普遍的需要用到的,所以必须掌握,而 之前我们一直说的都是怎么存. 你只会存不会取有个屁用.所以希望大家在如何查询读取数据这方面多下点功夫. 这篇和上一篇都是干货,我也是第一次学. ...
- MySQL数据库实验三 MySQL查询
一.实验项目: MySQL查询. 二.实验目的 掌握MySQL的查询操作. 三.实验内容 (一): 1.查询lineitem表中商品编号(productid)和单价(unitprice),要求消除重复 ...
- laravel mysql sum查询并排行_必看!PHP常见面试题——MySQL篇(二)
接上期:<必看!PHP常见面试题--MySQL篇(一)> 11.MySQL的默认事务隔离级别是? 读未提交(RU): 一个事务还没提交时, 它做的变更就能被别的事务看到. 读提交(RC): ...
- mysql revoke 用法_mysql进阶(二十八)MySQL GRANT REVOKE用法
mysql进阶(二十八)MySQL GRANT REVOKE用法 MySQL的权限系统围绕着两个概念: 认证->确定用户是否允许连接数据库服务器: 授权->确定用户是否拥有足够的权限执行查 ...
- MySQL的基本查询(二)
MySQL的基本查询(二) 文章目录 MySQL的基本查询(二) 3. Update 4. Delete 5. 插入查询结果 6. 聚合函数 7. group by子句的使用 3. Update 语法 ...
- 零基础带你学习MySQL—单行子查询和多行子查询(二十二)
零基础带你学习MySQL-多行子查询(二十二) 一.什么是子查询? 子查询是指嵌入在其它 sql 语句中的 select 语句,也叫嵌套查询 二.什么是单行子查询? 单行子查询是指只返回一行数据的子查 ...
- 零基础带你学习MySQL—查询数据库(二)
零基础带你学习MySQL-查询数据库(二) 如果数据库名字不是关键字,习惯性的不加反引号 哎呀 我就是懒,如果是关键字,必须要加上反引号 什么是关键字 我想大家应该都知道 我就不写了 哎呀 我就是懒
最新文章
- WCF优化的几个常规思路
- sql between包括两端吗_简单查询-SQL
- linux dip 命令详解
- 面向对象写单片机程序-程序分层
- linux openjdk_OpenJDK作为Linux上的默认Java
- MATLAB矩阵对称旋转
- 从标准到开发,解读基于MOF的应用模型管理
- 中文分词与马尔科夫模型之二:隐马尔科夫模型与维特比
- 百度中文依存句法分析工具DDParser重磅开源
- Python使用jieba库实现分词统计词频
- 检索计算机中word文件的词,电脑word搜索工具
- eos java调用命名sql_普元 EOS Platform 7.6 开发命名SQL缓存示例
- 八月十一上午笔记钊哥第一节课
- 美通企业日报 | 广州塔开业至今迎游客近1557万人次;居然之家成功借壳上市
- springboot整合德鲁伊
- 20X25 FCPX插件24种视频网站点赞订阅关注MG动画效果 Youtube Subscribe Generators
- 转载的一篇存储图片等信息的还不错的文章
- python图片内容识别_TensorFlow从1到2(五)图片内容识别和自然语言语义识别
- linux蓝牙打开自动关闭,firefly rk3288 蓝牙开启又自动关闭
- android多彩水果店设计,《多彩的水果王国》的教案设计
热门文章
- 为什么 MySQL 采用 B+ 树作为索引?
- 微信小程序电商项目总结(1)
- 孙陶然:什么是“三有”人才观
- 中文加解密异常的问题
- OSChina 周四乱弹 —— 如果你追到我,我就和你……
- 明日方舟 红松林故事集
- 日程规划软件哪个好 2022日程规划便签APP下载
- 优化PyTorch性能的一些trick
- SolidWorks的通过函数驱动绘制曲线
- 详解Git合并冲突——原因及解决 “Automatic merge failed; fix conflicts and then commit the result.“