mysql数据库(入门)三

is null

  1. 查询没有上级领导的员工编号姓名和工资 select empno,ename,sal from emp where mgr is null;
  2. 查询emp表中没有奖金comm的员工姓名ename,工资sal,奖金comm select ename,sal,comm from emp where comm is null;

is not null

  1. 查询emp表中有奖金的员工信息 select * from emp where comm is not null;

比较运算符

  1. 查询工资小于等于1600 姓名 工资 select ename,sal from emp where sal<=1600;
  2. 查询部门编号是20的员工姓名职位工资 select ename,job,sal from emp where deptno=20;
  3. 查询职位是manager的所有员工姓名职位部门编号 select ename,job,deptno from emp where job='manager'
  4. 查询不是10号部门的员工姓名部门编号 select ename,deptno from emp where deptno!=10; select ename,deptno from emp where deptno<>10;
  5. 查询商品表titem 单价等于23的商品信息 select * from titem where price=23;
  6. 查询单价不等于8443的商品信息 select * from t_item where price!=8443;
别名
  1. select ename as '姓名',sal as '工资' from emp;
  2. select ename '姓名',sal '工资' from emp;
  3. select ename 姓名,sal 工资 from emp;
去重 distinct
  1. 查询员工从事的所有职业 select distinct job from emp;
  2. 查询有员工的部门编号 select distinct deptno from emp;
and 和 or
  • and和java中的 &&效果一样
  • or 和java中的|| 效果一样
  • 查询10号部门工资高于3000块钱的员工信息 select * from emp where deptno=10 and sal>3000;
  • 查询部门编号为30或者上级领导为7698的员工姓名,职位,上级领导和部门编号 select ename,job,mgr,deptno from emp where deptno=30 or mgr=7698;
in
  1. 查询工资为5000,1500,3000的员工信息

    select * from emp where sal=5000 or sal=1500 or sal=3000; select * from emp where sal in (5000,1500,3000);

between x and y 在x和y之间 包含xy
  1. 查询工资在2000到4000之间的员工信息 select * from emp where sal between 2000 and 4000;
  2. 查询工资在2100到2800之外的员工姓名和工资 select ename,sal from emp where sal not between 2100 and 2800;
  3. 查询10号部门工资在2000到3000之间的员工信息

    select * from emp where deptno=10 and sal between 2000 and 3000;

模糊查询 like

  • _: 代表单个未知字符
  • %:代表0或多个未知字符
  • 举例:
  • 名字以a开头 ename like 'a%'
  • 以a结尾 %a
  • 包含a %a%
  • 第二个字母是a _a%
  • 倒数第三个字符是a %a__
  • 第二个字母是a最后字母是b _a%b
  • 案例:
  • 查询标题title中包含记事本的商品标题 select title from t_item where title like '%记事本%';
  • 查询单价低于100的记事本 标题和单价price select title,price from t_item where price<100 and title like '%记事本%';
  • 查询单价在50到200之间的得力(title包含得力)商品标题和单价 select title,price from t_item where price between 50 and 200 and title like '%得力%';
  • 查询有图片(image字段不等于null)的得力商品信息 select * from t_item where image is not null and title like '%得力%';
  • 查询有赠品的商品信息(sellpoint字段包含赠字) select * from titem where sell_point like '%赠%';
  • 商品标题中不包含得力的商品 select * from t_item where title not like '%得力%';

排序 order by

  • 如果有条件写在条件的后面 没条件写在 表名的后面
  • 默认是升序 desc降序 asc升序
  • 查询员工姓名和工资按照工资的降序排序 select ename,sal from emp order by sal desc;
  • 查询所有的dell商品(title包含dell) 按照单价降序排序 select title,price from t_item where title like '%dell%' order by price desc;
  • 多字段排序
  • 查询员工的姓名工资部门编号 按照部门编号降序如果编号相同则按照工资升序排序 select ename,sal,deptno from emp order by deptno desc,sal;

分页查询 limit x,y

  • 第一个参数代表跳过的条数
  • 第二个参数代表每页的数量
  • limit 关键字通常写在sql语句的最后面

  • 查询所有商品按照单价升序排序 显示第二页 每页7条数据 select * from t_item order by price limit 7,7;

  • 查询工资前三名的员工姓名和工资 select ename,sal from emp order by sal desc limit 0,3;

数值计算 + - * / 5%3等效mod(5,3)

  • 查询所有员工的姓名,工资及年终奖(工资5) select ename,sal,sal5 年终奖 from emp;
  • 查询商品表中商品单价,库存,及总金额(单价库存) select price,num,pricenum from t_item;

ifnull(x,y)函数

  • age=ifnull(x,y) 如果x的值为null则赋值y 如果不为null则赋值x
  • 将emp表中奖金为null的全部改成0 update emp set comm=ifnull(comm,0);

聚合函数

  • 对多行数据进行统计
  • 求和 sum(求和的字段名)
    • 查询所有员工的工资总和 select sum(sal) from emp;
    • 查询20号部门的工资总和 select sum(sal) from emp where deptno=20;
  • 平均值 avg(字段名)
    • 查询10号部门的平均工资 select avg(sal) from emp where deptno=10;
  • 最大值 max(字段名)
    • 查询30号部门的最高工资 select max(sal) from emp where deptno=30;
  • 最小值 min(字段名)
    • 查询dell商品中最便宜的商品价格 select min(price) from t_item where title like '%dell%';
  • 统计数量 count(字段名/*)
    • 查询工资大于等于3000的员工数量 select count(*) from emp where sal>=3000;

日期相关函数

select 'helloworld!';

  • 获取当前的年月日时分秒 now() select now();
  • 获取当前年月日 current select curdate();
  • 获取当前时分秒 select curtime();
  • 从年月日时分秒中 提取年月日 提取时分秒 select date(now()); select time(now());
  • 提取时间分量 年 月 日 时 分 秒 select extract(year from now()); select extract(month from now()); select extract(day from now()); select extract(hour from now()); select extract(minute from now()); select extract(second from now());
  • 日期格式化函数 -格式: date_format(日期,format);
  • format:
  • %Y 四位年 %y 两位年
  • %m 两位月 %c 一位月
  • %d 号
  • %H 24小时 %h 12小时
  • %i 分
  • %s 秒 select now();
  • 把now()格式改成 年月日时分秒 select date_format(now(),'%Y年%m月%d日%H时%i分%s秒');

  • 把非标准格式转回标准格式 strtodate(非标准时间,format)

  • 14.08.2018 08:00:00 转回标准时间 select strtodate('14.08.2018 08:00:00','%d.%m.%Y %H:%i:%s');
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值