SQL常见命令二

  • 条件查询案例
select
	last_name,
	department_id,
	salary * 12 *(1+isnull(commission_pct, 0)) as 年薪
from
	employees;

#案例:查询没有奖金,且工资小于18000,last_name
select
	salary,last_name
from 
	employees
where
	commission_pct is null and salary < 18000;
#案例:查询employees表中,job_id不为'IT'或者工资为12000的员工信息
select *
	from employees
where 
	job_id <> 'IT' or salary=12000;
#案例:查看部门departments表的结构
desc departments;
#案例:查询部门departments表中涉及到了哪些位置编号
select distinct location_id
from departments;
#案例:
select * from employees; 
select * from employees where commission_pct like '%%' and last_name like '%%';
select * from employees where commission_pct like '%%' or last_name like '%%' or employee_id like '%%';
  • 排序查询
    语法:
    select 查询列表
    from 表
    where 筛选条件
    order by 排序列表 [asc|desc]
    特点:
    asc代表的是升序,desc代表的是降序,如果不写,默认是升序
    order by子句中可以支持单个字段、多个字段、表达式、函数、别名
    order by子句一般是放在查询语句的最后面,limit子句除外
#案例1:查询员工信息,要求工资从高到低排序
select * from employees order by salary desc;
select * from employees order by salary;
#案例2:查询部门编号>=90的员工信息,按入职时间的先后进行排序[添加筛选条件]
select * 
from employees
where department_id>=90
order by hiredate asc;
#案例3:按年薪的高低显示员工的信息和年薪[按表达式排序]
select
	*,salary*12*(1+ifnull(commission_pct, 0)) 年薪
from
	employees
order by
	salary * 12 * (1 + ifnull(commission_pct,0)) desc;
#案例4:按年薪的高低显示员工的信息和年薪[按别名排序]
select 
	*,salary*12*(1+ifnull(commission_pct, 0)) 年薪
from
	employees
order by 年薪 desc;
#案例5:按姓名的长度显示员工的姓名和工资[按函数排序]
select 
	length(last_name) 字节长度,last_name,salary
from
	employees
order by
	length(last_name) desc;
#案例6:查询员工信息,要求先按工资升序,再按员工编号降序[按多个字段排序]
select *
from employees
order by salary asc,employee_id desc;
  • 案例
#案例1:查询员工的姓名和部门号和年薪,按年薪降序 按姓名升序
select last_name,department_id,salary*12*(1+ifnull(commission_pct, 0)) 年薪
from employees
order by 年薪 desc, last_name asc;
#案例2:选择工资不在8000到17000的员工的姓名和工资,按工资降序
select last_name,salary
from employees
where salary not between 8000 and 17000
order by salary desc;
#案例3:查询邮箱中包含e的员工信息,并先按邮箱的字节数降序,再按部门号升序
select *,length(email)
from employees
where email like '%e%'
order by length(email) desc,department_id asc;
  • 常见函数
    调用:select 函数名(实参列表) [from表];
    分类:
    1、单行函数
    如concat、length、ifnull等
    2、分组函数
    功能:做统计使用,又称为统计函数、聚合函数、组函数
  • 字符函数
#1、length 获取参数值的字节个数
select length('john');
select length('张三丰hahaha');
show variables like '%char%'
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值