一.简单查询--select
1.用处:查询单一字段、查询连续的多个字段、查询所有字段
2.用法:
查询字段 | 语句 | 备注 |
单一字段 | select name from userInfo; |
关键字:select、from;标识符:字段名、表名
|
连续多个字段 | select name,age,email from userInfo; |
/ |
所有字段 | select * from userInfo; |
缺点:效率低、可读性差 |
二.条件查询--where
1.什么是条件查询?
仅查询复合条件的数据。
语法格式:select 字段1,字段2,字段3 from 表名 where 条件;
2.条件查询的分类:
共计13种,分别为:>, <, >=, <=, =, <>或!=, between...and... , is null, and, or, in( , , , ), not in( , , , ), not
含义 | 分类 | 备注 |
等于 | = | / |
不等于 | <>或!= | / |
两值之间 | between...and... | 等价于:>= and <= |
记录查询为null的记录 | is null | 数据库中null不能使用 = 进行衡量,需要使用is null ∵ 数据库中的null代表什么也没有,不是一个值 |
取非 | not | 主要用在 is null 和 in 中 |
并且 | and | and优先级比or高,若需or先执行,需要加“()” |
或者 | or | |
包含 | in( , , , ) |
in相当于多个or。in不是一个区间。in后面跟的是具体值。 |
不在()内数值中的几个值的数据 | not in( , , , ) | select ename,sal from emp where sal not in(800, 5000, 3000); |
分类 | 语句 | 备注 |
= | select sal from emp where ename = 'SMITH'; | 字符串使用单引号 |
<> / != | select ename from emp where sal != 800; | ... sal <> 800; |
between..and.. | select ename,sal from emp where sal between 2450 and 3000; | 左小右大,闭区间 |
is null | select ename,sal,comm from emp where comm is null; | / |
and | select ename,job,sal from emp where job = 'tech' and sal > 2500; |
/ |
or | select ename,job from emp where job = 'tech' or job = 'salsman'; | / |
in( , , , ) | select ename,job from emp where job in ('tech', 'salsman'); | 括号内是具体值 |
三.模糊查询--like
1.支持 % 或 _ 匹配。
%:匹配任意 多个 字符 开头、结尾标识符
_:匹配任意 一个 字符 占位符
2.示例: