mysql两表都有条件_mysql单表,多表,符合条件,⼦查询
单表:
HAVING过滤
⼆次筛选 只能是group by 之后的字段
1.查询各岗位内包含的员⼯个数⼩于2的岗位名、岗位内包含员⼯名字、个数
select post,group_concat(name),count(1) from employee group by post having count(1)<2;
2. 查询各岗位平均薪资⼤于10000的岗位名、平均⼯资
select post,avg(salary) as a from employee group by post having a >10000;(怕avg为关键字,⽤as a 代替)
3. 查询各岗位平均薪资⼤于10000且⼩于20000的岗位名、平均⼯资专业java培训机构
select post,avg(salary) as a from employee group by post having a>10000 and a<20000;
order by 查询排序
排序 order by age desc / asc
1. 查询所有员⼯信息,先按照age升序排序,如果age相同则按照hire_date降序排序
decimal在mysql中怎么使用select * from employee order by age asc,hire_date desc;
2. 查询各岗位平均薪资⼤于10000的岗位名、平均⼯资,结果按平均薪资升序排列
mysql语句多表查询select post,avg(salary) from employee group by post having avg(salary)>10000 order by avg(salary) asc; (order by 后⾯⼀定要有参数)
limit 限制查询的记录数:
select * from employee order by salary desc limit 5,5; (默认为0
从排好序的第五条开始,5个)
多表查询
多表连接查询
select * from employee,department where employee.dep_id = department.id
外链接操作:
1.语法
inner /left/right join (显⽰全部 有nul不显⽰,优先左边,没有关联的表为null/右边优先)
select * from employe inner join department on employee.dep_id = department.id
全外链接 union
select * from employee left join department on employee.dep_id = department.id
union
select * from employee right join department on employee.dep_id = department.id
符合条件查询
以内连接的⽅式查询employee和department表,并且employee表中的age字段值必须⼤于25,
即出年龄⼤于25岁的员⼯以及员⼯所在的部门
加拿大起诉吴亦凡select employee.name,department.name from employee inner join department on employee.dep_id = department.id where age >25;
(on 后⾯跟着where 条件)
1、查询平均年龄在25岁以上的部门名
select name from department where id in(select dep_id from employee group by dep_id having avg(age) >25) ;
通过在emp 表中到的dep_id 对应上 dep 表中的id ⽽到name
2、查看不⾜1⼈的部门名
select name from department where id not in(select dep_id from employee group by dep_id) ;
直方图怎么做数据分析(利⽤共同的dep_id来判断)
3、查询⼤于所有⼈平均年龄的员⼯名与年龄
select name,age from employee where age> (select avg(age) from employee);
(这⾥的是员⼯名和年龄 跟部门没有关系,,,)
4、查询⼤于部门内平均年龄的员⼯名、年龄
1)先对员⼯表(employee)中的⼈员分组(group by),查询出dep_id以及平均年龄。
(2)将查出的结果作为临时表,再对根据临时表的dep_id和employee的dep_id作为筛选条件将employee表和临时表进⾏内连接。
(3)最后再将employee员⼯的年龄是⼤于平均年龄的员⼯名字和年龄筛选。
select * from employee inner join (select dep_id,avg(age) as bfrom employee group by dep_id) as A on employee.dep_id = A.dep_id where employee.age>A.b;
5.查询每个部门最新⼊职的那位员⼯
select name from employee inner join( select post,max(hire_date) as newtime from employee group by post) as A on employee .post = A.post where employee.hire_wtime;
同上,都是关于进⾏ on dep_id 的判断 ,第⼆个则是关于where 的判断
pymysql模块的使⽤php是哪里来的
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论