600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > MySQL数据库中where条件查询(比较运算符查询 逻辑运算符查询 模糊查询 范围查询

MySQL数据库中where条件查询(比较运算符查询 逻辑运算符查询 模糊查询 范围查询

时间:2021-01-29 19:18:01

相关推荐

MySQL数据库中where条件查询(比较运算符查询 逻辑运算符查询 模糊查询 范围查询

1. where条件查询的介绍

where条件查询可以对表中的数据进行筛选,条件成立的记录会出现在结果集中。

where语句支持的运算符:

比较运算符逻辑运算符模糊查询范围查询空判断

where条件查询语法格式如下:

select * from 表名 where 条件;例:select * from students where id = 1;

2. 比较运算符查询

等于: =大于: >大于等于: >=小于: <小于等于: <=不等于: != 或 <>

1:查询编号大于3的学生:

select * from students where id > 3;

2:查询编号不大于4的学生:

select * from students where id <= 4;

3:查询姓名不是“黄蓉”的学生:

select * from students where name != '张飞';

4:查询没被删除的学生:

select * from students where isdelete=0;

3. 逻辑运算符查询 and or not

逻辑运算符and表示多个条件同时成立则为真,or表示多个条件有一个成立则为真,not表示对条件取反

1:查询编号大于3的女同学:

select * from students where id > 3 and gender='女';

2:查询编号小于4或没被删除的学生:

select * from students where id < 4 or isdelete=0;

3:查询年龄不在19岁到25岁之间的学生:

select * from students where not (age >= 19 and age <= 25);

说明:

多个条件判断想要作为一个整体,可以结合‘()’。

4. 模糊查询

like是模糊查询关键字%表示任意多个任意字符_表示一个任意字符

1:查询姓张的学生:

select * from students where name like '张%';

2:查询姓张并且“名”是一个字的学生:

select * from students where name like '张_';

3:查询姓张或叫羽的学生:

select * from students where name like '张%' or name like '%羽';

5. 范围查询

between .. and .. 表示在一个连续的范围内查询in 表示在一个非连续的范围内查询 【例如:select * from students where id in (3, 5,7,9);】

1:查询编号为2至4的学生:

select * from students where id between 2 and 4;

【上面代码等价于 select * from students where id >= 2 and id <= 4;】

2:查询编号不是2至4的男生:

select * from students where (not id between 2 and 4) and gender='男';

3:查询编号是2和4的学生:

select * from students where id in (2, 4);

6. 空判断查询

判断为空使用: is null判断非空使用: is not null

1:查询没有填写身高的学生:

select * from students where height is null;

注意:

不能使用 where height = null 判断为空不能使用 where height != null 判断非空null 不等于 '' 空字符串

MySQL数据库中where条件查询(比较运算符查询 逻辑运算符查询 模糊查询 范围查询 空判断查询)

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。