美文网首页
mysql进阶2:条件查询

mysql进阶2:条件查询

作者: 弦好想断 | 来源:发表于2020-04-19 19:18 被阅读0次

/*
语法:select 查询列表 from 表名 where 筛选条件
1.按条件表达式筛选
条件运算符:> < = != <> >= <=
2.按逻辑表达式筛选
逻辑运算符:用于连接条件表达式
&& || !
and or not
3.模糊查询
like:一般和通配符搭配使用
%:任意多个字符(包含0个) _:任意单个字符
between and in is null
*/

1.按条件表达式筛选

案例:查询工资大于12000的员工信息

select
        *
from 
        employees
where
        salary>=12000;

案例2:查询部门编号不等于90的员工名和部门编号

select last_name,department_id from employees where department_id !=90;
select last_name,department_id from employees where department_id <>90;

2.按逻辑表达式筛选

案例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>110 or salary>15000;
select * from employees where not(department_id>=90 and department_id<=110) or salary>15000;

3.模糊查询

案例1:查询员工名中包含字符a的员工信息

select * from employees where last_name like '%a%';

案例2:查询员工名中第三个字符为n,第无个字符为l的员工名和工资

select last_name,salary from employees where last_name like '__n_l%';

案例3:查询员工名中第二个字符为_的员工名

select last_name from employees where last_name like '_\_%';

使用escape指定任意字符进行转义

select last_name from employees where last_name like '_a_%' escape('a');

2.between and(包含临界值,两个临界值不要调换顺序)

案例1:查询员工编号在100到120之间的员工信息

select * from employees where employee_id>=100 and employee_id<=120;
#-----------------
select * from employees where employee_id between 100 and 120;

3. in关键字

/*
含义:用于某字段的值是否属于in列表中的某一项
特点:使用in提高语句简度 in列表的值类型必须统一或兼容 不支持通配符
*/

查询员工的工种编号是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');

4. is null

案例:查询没有奖金的员工名和奖金率

select last_name,commission_pct from employees where commission_pct is null;
select last_name,commission_pct from employees where commission_pct is not null;

5.安全等于<=>

案例一:查询没有奖金的员工名和奖金率

select last_name,commission_pct from employees where commission_pct <=> null;

案例2:查询工资为12000的员工信息

select * from employees where salary <=> 12000;

/*
is null PK <=>
is null :仅仅能判断null值
<=>即能判断null值,也能判断数值型字符
*/

查询员工号为176的员工姓名和部门号和年薪

select employee_id,concat(last_name,first_name) as 姓名,department_id,salary*12*
(1+ifnull(commission_pct,0)) as 年薪 from employees where employee_id <=> 176;

测试

1.查询没有奖金,且工资小于18000的salary,last_name

select salary,last_name from employees where commission_pct is null and salary<18000;

2.查询employees表中,job_id不为‘it’或者工资为12000的员工信息

select * from employees where job_id <>'it'

相关文章

  • mysql进阶2:条件查询

    /*语法:select 查询列表 from 表名 where 筛选条件1.按条件表达式筛选条件运算符:> < = ...

  • mysql条件查询

    进阶2:条件查询 /* 语法:select查询列表from表名where筛选条件; 分类:一、按条件表达式筛选 *...

  • #进阶2:条件查询

    /*语法: select 查询列表 from 表名 where ...

  • 进阶2:条件查询

    尚硅谷:https://www.bilibili.com/video/BV1xW411u7ax?p=1[https...

  • 数据库操作

    2019年12月10日 mysql -uroot -pwy123456 一.查询: 1.有条件查询 2.无条件查询...

  • mysql 查询 - 个人记录

    1.mysql 子查询使用父查询字段作为条件,查询B表数量 2.mysql 根据某字段排行,获取当前用户排名

  • mysql删除重复数据

    Mysql 查询重复数据: 1.单条件查询: 2.多条件查询: 3.删除重复数据,只保留最小ID的数据:(多条件)

  • 面试总结

    1.Java编程 2. Mysql添加索引后查询速度没有变化 mysql查询语句的where条件字段和select...

  • 11django模型[条件查询]

    字段查询查询mysql数据库日志条件查询1) 查询等2) 模糊查询3) 空查询4) 范围查询5) 比较查询6) 日...

  • MySQL实战 目录

    MySQL实战 MySQL实战1 数据库概念介绍MySQL实战2 语法、筛选条件和函数MySQL实战3 分组查询和...

网友评论

      本文标题:mysql进阶2:条件查询

      本文链接:https://www.haomeiwen.com/subject/nfpbbhtx.html