MySQL使用教程(基础篇04)

1、条件查询介绍

分类:

  • (1)按条件表达式刷选;条件运算符;
  • (2)按逻辑表达式刷选;
  • (3)模糊查询

2、条件运算符的使用

#案例1:查询工资》12000的员工信息
select * from employees where salary>12000;

#案例2:查询部门编号不等于90的员工名和部门编号
select last_name,department_id from employees where department_id!=90;

3、逻辑运算符的使用

#案例1:查询工资在10000到20000之间的员工名、工资和奖金
select last_name,salary,commission_pct from employees where salary>=10000 and salary<=20000;

#案例2:查询部门编号不是在90到110之间,或者工资高于15000的员工信息
select * from employees
where department_id<90 or department_id>100 or salary>15000;

4、模糊查询_like关键字

like:
特点:

1)一般和通配符搭配使用
通配符:% 任意多个字符,包含0个字符
_任意单个字符

#案例1:查询员工名中包含字符a的员工信息
select * from employees where last_name like '%a%';

#案例2:查询员工名中第三个字符为e,第五个字符为a的员工名和工资
select last_name,salary from employees where last_name like '__n_a%';

#案例3:查询员工名中第二个字符为_的员工名
select last_name from employees where last_name like '_\_%';

select last_name from employees where last_name like '_$_%' escape '$';

5、模糊查询_between and

between and:
特点:

   1)使用between and可以提高语句的简洁渡
   2)包含临界值
   3)两个临界值不要调换顺序

6、模糊查询_in关键字

in:
特点:

1)使用in提高语句简洁度;
2)in列表的值类型必须统一或兼容

#案例1:查询员工的工种编号是IT_PROT AD_VP AD_PRES中的一个员工名和工种编号
select last_name,job_id from employees where job_id='IT_PROT' or job_id='AD_VP' or job_id='AD_PRES';

-- 方式二
select last_name,job_id from employees where job_id in('IT_PROT','AD_VP','AD_PRES');

7、模糊查询_is null关键字

is null:=或<>不能用于判断null值
is null或is not null可以判断null值

#案例1:查询没有奖金的员工名和奖金率
select last_name,commission_pct
from employees
where commission_pct is null;

8、补充 安全等于的介绍

安全等于 <=>:

is null:仅仅可以判断null值; 可读性高 建议使用
<=>:既可以判断null值,又可以判断普通的数值; 可读性低

#案例1:查询没有奖金的员工名和奖金率 
select last_name,commission_pct
from employees
where commission_pct <=>NULL;

#案例2:查询工资为12000的员工信息 
select last_name,salary
from employees
where salary <=>12000;

9、排序查询介绍

语法:
select 查询列表
from 表
【where 刷选条件】
order by 排序列表 【asc|desc】

特点:

1、asc代表的是升序,desc代表的是降序
2、如果不写,默认是升序

#案例:查询员工信息,要求工资从高到低 
select * from employees order by salary desc;

10、排序查询示例

#案例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;

11、排序查询总结

语法:
select 查询列表
from 表
【where 刷选条件】
order by 排序列表 【asc|desc】

特点:

1、asc代表的是升序,desc代表的是降序
2、如果不写,默认是升序
3、order by子句中可以支持单个字段,多个字段,表达式,函数,别名
4、order by子句一般是放在查询语句的最后面,limit子句除外

12、常见函数介绍

概念: 类似于java的方法,将一组逻辑语句封装在方法体中,对外暴露方法名

好处: 1、隐藏了实现细节 2、提高代码的重用性

调用: select 函数名(实参列表) 【from 表】

分类:

1、单行函数
如concat、length、ifnull等
2、分组函数
功能:做统计使用,又称为统计函数、聚合函数、组函数

13、字符函数

#一、字符函数 
#length		获取参数值的字节个数 
select length('join');
select length('张三丰hahaha');  
show variables like '%char%';

#2、concat	拼接字符串 
select concat(last_name,'_',first_name) 姓名 from employees;

#3、upper、lower
select upper('join');
select lower('joJn');

#示例 :将姓变大写,名变小写,然后拼接 
select concat(upper(last_name),lower(first_name)) 姓名 from employees;

#4、substr、substring
#注意:索引从1开始
#截取从指定索引处后面的所有字符 
select substr('李莫愁爱上了陆展元',7) out_put;

#截取从指定索引处指定字符长度的字符 
select substr('李莫愁爱上了陆展元',1,3) out_put;

#案例:姓名中首字符大写,其他字符小写然后用_拼接,显示出来 
select concat(upper(substr(last_name,1,1)),'_',lower(substr(last_name,2))) out_put from employees;

#5、instr 返回子串第一次出现的索引,如果找不到返回0 
select instr('张一山喜欢杨紫','杨紫') out_put;

#6、trim
select length(trim('		马龙	')) as out_put;
select trim('aa' from 'aaaaaaa张aaaaaaaaaa计科aaaaaaa') as out_put;

#7、lpad 用指定的字符实现左填充指定长度 
select lpad('东方不败',2,'*')  as out_put;

#8、rpad用指定的字符实现右填充指定长度 
select rpad('张继科',12,'ab') as out_put;

#9、replace替换 
select replace('小希小希小哈','小希','xiaoen')  as out_put;

14、数学函数

#二、数学函数 
#round 四舍五入
select round(-1.55);
select round(1.34);
select round(1.567,2);

#ceil 向上取整 返回>=该参数的最小整数 
select ceil(-1.02);

#floor 向下取整 返回<=该参数的最大整数 
select floor(-9.99);

#truncate 截断 
select truncate(1.69999,1);

#mod 取余 
/*
    mod(a,b) :a-a/b*b
*/

select mod(10,-3);
select 10%3;

15、日期函数

#三、日期函数 
#now 返回当前系统日期+时间 
select now();

#curdate 返回当前系统日期,不包含时间 
select curdate();

#curtime 返回当前时间,不包含日期 
select curtime();

#可以获取指定的部分,年、月、日、小时、分钟、秒 
select year(now());
select year('1998-1-1');

select year(hiredate)from employees;

select month(now());
select monthname(now());

#str_to_date 将字符通过指定的格式转换成日期 
select str_to_date('1998-2-2','%Y-%c-%d') as out_put;

#查询入职日期为1992-4-3的员工信息 
select * from employees where hiredate='1992-4-3';

select * from employees where hiredate=str_to_date('4-3 1992','%c-%d %Y');

#date_fromat 将日期转换成字符 
select date_format(now(),'%y年%m月%d日') as out_put;

#查询有奖金的员工名和入职日期(xx月/xx日 xx年) 
select last_name,date_format(hiredate,'%m月/%d日 %y年') 入职日期 
from employees
where commission_pct is not null;
  • 8
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值