查询一个字段
select 字段名 from 表名
查询多个字段
select 字段1,字段2,字段3 from 表名
查询所有字段
- 把所有字段都写上
select 字段1,字段2,字段3 from 表名
- 第二种方式,使用*
select * from 表名
推荐使用第一种,第二种方式可读性差,效率低,不推荐使用
条件查询
select 字段1,字段2,字段3
from 表名
where 条件
有哪些条件?:
-
等于 =
-
不等于 <> 或者 !=
-
小于 <
-
大于 >
-
小于等于 <=
-
大于等于 >=
-
值在…和…之间 between…and…(注意:使用between必须左小右大) 等同于 >= and<=
-
为空 is null
-
不为空 is not null
注意:数据库中的null不可以使用=判断,要使用is,或者使用is not null -
并且 and
-
或者 or
注意:and和or同时出现and优先级较高,如果想让or先执行可以加()
如果以后开发不确定优先级,可以加() -
包含 in 注意:in不是一个区间,是具体的值,可以加一个或者多个 如:
查询价格是5000,3300的商品名字
select name from shop where price in 5000,3300
- 取非 主要和null和in使用
- 模糊查询 like 支持 % 或下划线匹配(下划线’_'表示任意一个字符,%表示任意多个)如:
找出商品名字含有李白的
select * from shop where name like '%李白%';
找出商品名字以tom开始的
select * from shop where name like 'tom%';
找出价格是00结尾的
select * from shop where price like '%00';
找出名字第二个字母是a的
select * from shop where name like '_a%';
特殊情况:找出名字中带"_"的?
select * from shop where name like '%_%'; ??????????????? 错误
有特殊含义的字符需要使用\转义
select * from shop where name like '%\_%';