MySQLDQL之条件查询
Posted leeqico
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQLDQL之条件查询相关的知识,希望对你有一定的参考价值。
1、语法:select 查询列表 from 表名 where
2、筛选条件分类:
(1)按条件表达式筛选
简单条件运算符:>、 < 、= 、!= 、<>、 >=、 <=
(2)按逻辑表达式筛选
逻辑运算符:用于连接条件表达式
&&、 ||、 !
and、 or 、not
&&和and:两个条件都为true,结果为true,反之为false
||或or: 只要有一个条件为true,结果为true,反之为false
!或not: 如果连接的条件本身为false,结果为true,反之为false
(3)模糊查询
like、between and、in、is null
3、按条件表达式筛选
案例1:查询工资>12000的员工信息
SELECT * FROM employees WHERE salary>12000;
案例2:查询部门编号不等于90号的员工名和部门编号
SELECT last_name,department_id FROM employees WHERE department_id<>90;
4、按逻辑表达式筛选
案例1:查询工资在10000到20000之间的员工名、工资以及奖金
![技术分享图片](https://upload-images.jianshu.io/upload_images/2058461-004716043cc52f82.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
案例2:查询部门编号不是在90到110之间,或者工资高于15000的员工信息
![技术分享图片](https://upload-images.jianshu.io/upload_images/2058461-22722dacc781e9ed.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
5、模糊查询
(1)like关键字
特点:一般和通配符搭配使用。
通配符:% 任意多个字符,包含0个字符;
_ 任意单个字符。
案例1:查询员工名中包含字符a的员工信息
select * from employees where last_name like ‘%a%‘;
案例2:查询员工名中第三个字符为n,第五个字符为l的员工名和工资
![技术分享图片](https://upload-images.jianshu.io/upload_images/2058461-8588891a5c35c7fb.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
案例3:查询员工名中第二个字符为_的员工名
![技术分享图片](https://upload-images.jianshu.io/upload_images/2058461-e95fa43e0b299b5b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
其中$可以为任意字符
或者
![技术分享图片](https://upload-images.jianshu.io/upload_images/2058461-790ff667b6431d91.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
(2)between and关键字
①使用between and 可以提高语句的简洁度
②包含临界值
③两个临界值不要调换顺序
案例1:查询员工编号在100到120之间的员工信息
![技术分享图片](https://upload-images.jianshu.io/upload_images/2058461-7f5d6bf63ca03591.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
(3)in关键字
含义:判断某字段的值是否属于in列表中的某一项
特点:
①使用in提高语句简洁度
②in列表的值类型必须一致或兼容
③in列表中不支持通配符
案例:查询员工的工种编号是 IT_PROG、AD_VP、AD_PRES中的一个员工名和工种编号
![技术分享图片](https://upload-images.jianshu.io/upload_images/2058461-ffd26e284a6be2ca.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
(4)is null和is not null关键字
=或<>不能用于判断null值
is null或is not null 可以判断null值
案例1:查询没有奖金的员工名和奖金率
![技术分享图片](https://upload-images.jianshu.io/upload_images/2058461-4dafcf857f189692.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
案例2:查询有奖金的员工名和奖金率
![技术分享图片](https://upload-images.jianshu.io/upload_images/2058461-a263f50cc78739e1.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
(5)安全等于 <=>
案例1:查询没有奖金的员工名和奖金率
![技术分享图片](https://upload-images.jianshu.io/upload_images/2058461-30ce53deb74994f9.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
案例2:查询工资为12000的员工信息
![技术分享图片](https://upload-images.jianshu.io/upload_images/2058461-ea7a0c93c4e581f5.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
IS NULL:仅仅可以判断NULL值,可读性较高,建议使用
<=> :既可以判断NULL值,又可以判断普通的数值,可读性较低
以上是关于MySQLDQL之条件查询的主要内容,如果未能解决你的问题,请参考以下文章