Mysql 查询语句


1. 表的全查, 不带条件的查询
select * from hzuser;
2. 条件查询, 条件一共有: 13个 , 去掉相似的 共8个
查询 id 是 5484的人是谁?   =
select * from hzuser where hzUserNum = 5484;
查询 id 不是 5484的人 有哪些?  !=
select * from hzuser where hzUserNum != 5484;
select * from hzuser where hzUserNum <> 5484;
查询 工资大于3000的人有哪些?
select * from hzuser where userSal>3000;
查询 工资大于3000 到 5000 的人有哪些?  
BETWEEN ...and .... 相当于 >= <=  一个区间值
select * from hzuser where userSal BETWEEN 3000 and 5000;
查询 绩效comm 是null 的员工有哪些?
select * from hzuser where comm is null;
查询 绩效comm 不是null 的员工有哪些?
select * from hzuser where comm is not null;
查询职业job 是备胎 且 工资大于 1300 的人有那些   AND
select * from hzuser where job='备胎' and usersal > 1300;
查询职业job 是 备胎 或者是  太监的人有哪些? 用 or 或者 in
select * from hzuser where job='备胎' or job='太监'
select * from hzuser where job in ('备胎', '太监' )
总结: between and 可以 用  >= x and <=y 来代替
      or 可以用  in 来代替
like 模糊查询 查询 名字中有小的 人 有哪些?
select * from hzuser where name like '%小%'
聚合函数:
count 求总条数
查询这个表有多条记录
select count(*) from hzuser;
count 求总条数 可以 和where 一起使用
比如说: 求 工资大于 3000人有多少?
select count(*) from hzuser WHERE userSal> 3000 ;
练习: 查询 工资大于1500 并且 职业是 太监的人 有多少条 ?
select count(*) from hzuser WHERE userSal>1000  and job ='太监';
查询comm 绩效 不是null 的人 有多少个?
select count(*) from hzuser WHERE comm is not null;
查询comm 绩效  是null 的人 有多少个?
select count(*) from hzuser WHERE comm is null;
SUM 求和
比如: 求 员工的comm绩效 一共有多少钱?
select SUM(comm) from hzuser WHERE comm is not null; //3832.00
AVG 平均数
19个员工 他的平均绩效是多少?
select  DISTINCT 3832.00/19 from hzuser;  //201.684211 19个人的平均
select Avg(comm) from hzuser; // 425  和上面的数据不对等
select Avg(ifnull(comm,0)) from hzuser; 201.684211 和上面就对等了
注意:ifnull 如果 为0 ...
查询出 备胎和太监 的绩效的平均值是多少 ?
select Avg(ifnull(comm,0)) from hzuser where job='备胎'; //550
select Avg(ifnull(comm,0)) from hzuser where job='太监';  //0
select SUM(ifnull(comm,0)) from hzuser where job='备胎'; //2200.00
select SUM(ifnull(comm,0)) from hzuser where job='太监'; //0.00
select count(*) from hzuser where job='备胎' ; //4
select count(*) from hzuser where job='太监' ;  //4
select 2200/8 from hzuser; //275

select Avg(ifnull(comm,0)) from hzuser where job='备胎' or  job='太监' ;
select Avg(ifnull(comm,0)) from hzuser where job in('备胎'  ,  '太监') ;

Max / MIN 最大值, 最小值
最工资最高的是多少?
select MAX(userSal) from hzuser ;// 注意 他是一个 数字
// 是一个 单行单列的一条数据. 他就不能和 hzuser表进行 并列了..
// 那么 他就是一个数值
// 总结注意: 单个的聚合函数不可以和 表的字段名也一起写.
练习:  求出 工资最高的人的名字和最高的工资数字.
select * from hzuser where userSal =  (select MAX(userSal) from hzuser ) ;

分组函数, having 必须跟在 group by 后面, 
没有 group by 就没有having
那么 having 就是 一个高级版的 where 

1.什么是分组. 一样的数据 就可以认为是一组
那么 丫鬟 可以是一组, 备胎可以是一组,
jiejinum 等于 30  可以是一组
根据 job工作岗位 来分组,
select job from hzuser group by job;
注意: groub by 后面的 列 和前面的 select 后面的 列
名字一般保持一致 ,,不一致可能会出现不对的数据.
按 工资多少分组
select usersal  from hzuser group by usersal;
注意: group by 多和 聚合函数一起使用.
比如说, 查询 每个岗位工资最高的数字是多少?
select job , Max(usersal) from hzuser group by job;
查询 太监岗位工资最高的数字是多少?
select job , Max(usersal) from hzuser group by job having job='太监';

多表查询
自链接 , 1张表 当多个表看
查询 员工姓名和他上级领导的姓名   
select a.name as '领导' , b.name as '下属' from hzuser a , hzuser b where a.hzuserNum = b.manager (不建议这么写)
select  a.name as '领导' , b.name as '下属' from hzuser a join hzuser b on a.hzuserNum = b.manager
左右链接
select  a.name as '领导' , b.name as '下属' from hzuser a  right join hzuser b on a.hzuserNum = b.manager

select  a.name as '领导' , b.name as '下属' from hzuser a  left join hzuser b on a.hzuserNum = b.manager

2个表的链接
查询 员工工资usersal 大于2000 并且显示他的住在哪里.
select * from hzuser a join jieji b on a.jiejinum = b.jiejinum
where a.usersal>2000

子查询在where 中的使用, 可以看作一个列
比如: 查询hzuser表, 那些人是领导,要求显示编号和姓名
1.查询出 领导编号 去重复
select DISTINCT manager from hzuser where manager is not null;
2. 查询会员编号 包涵领导
select hzusernum,name from hzuser where hzusernum 
in (select DISTINCT manager from hzuser where manager is not null);

子查询在from中的使用  可以看作一张表
 查询hzuser表, 那些人是领导,要求显示编号和姓名
select hz.hzusernum, hz.name from hzuser hz  join 
(select DISTINCT manager from hzuser where manager is not null) m 
on hz.hzusernum=m.manager

子查询在select中的使用  可以看作一个列名 不常用
比如:查询会员信息,并显示出会员所属的阶级名称.
1. 查询出阶级的名称
  select hz.name , (select j.jname from jieji j where  hz.jiejinum=j.jiejinum )as jname from hzuser hz;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值