Mysql数据库操作指南——条件查询(零基础篇九)
数据规模增大后,全表扫描既耗时又浪费缓冲池。MySQL 通过 WHERE 子句对行级数据进行过滤,仅返回满足条件的记录。本章系统介绍比较运算符、逻辑运算符、区间与集合匹配五种基础写法,帮助读者在单表层面实现精确过滤。
条件查询:
语法:select 字段1,字段2,字段3,字段4,字段5......from 数据表名字 where 查询条件;
where后续可接:
| 符号 | 解释 | 示例 |
| < | 小于 | select * from star where age < 96; |
| > | 大于 | select * from star where age > 96; |
| >= | 大于等于 | select * from star where age >= 96; |
| <= | 小于等于 | select * from star where age <= 96; |
| !=或<> | 不等于 |
select * from star where age != 96; select * from star where age <> 96; |
| = | 等于 | select * from star where age = 96; |
| or | 或者 | select * from star where age = 96 or province = "河南省" ; |
| and | 并且 | select * from star where age < 96 and province = "甘肃省"; |
| between and | 在某个区间[ ] |
select * from star where id between 2 and 5; select * from star where age between 87 and 98; |
| in /not in | 在/不在指定的集合中 |
select * from star where id in (1,4,3,5); select * from star where id not in (1,3,5); |
| like | 模糊查询 %通配符 |
select * from star where name like '张%'; select * from star where name like '%天%'; |
代码依次为:
select * from star where age < 96;
select * from star where age > 96;
select * from star where age >= 96;
select * from star where age <= 96;
select * from star where age != 96;
select * from star where age <> 96;
select * from star where age = 96;
select * from star where age = 96 or province = "河南省" ;
select * from star where age < 96 and province = "甘肃省";
select * from star where id between 2 and 5;
select * from star where age between 87 and 98;
select * from star where id in (1,4,3,5);
select * from star where id not in (1,3,5);
select * from star where name like '张%';
select * from star where name like '%天%';
示例:



总结:
建议初学者依据本章内容进行练习,熟悉过滤条件查询。











