查询语句
SELECT 列名称 FROM 表名称;
-- 查询单独的数据
SELECT * FROM 表名称
-- 查询所有的数据
- SELECT 列名称 FROM 表名称;
- 查询某列的数据
- SELECT * FROM 表名称
- 查询表中所有的数据 *:代表所有的列
1.使用比较运算符
- =
- >
- <
=
- <=
- != <> 不等于
- !< 不小于
- !> 不大于
语法
-- 查询大于90分的学生
select * from score where score >= 90;
- 筛选的条件需要添加到我们where的后面
2.多个条件筛选
- or
- and
- not
- between
- not between
and :并且 需要同时满足这个条件
-- 查询成绩大于90 并且 姓名是王五 and
select * from score where score >= 90 and user_name = '王五';
or:或者 满足其中的一个条件
-- or 查询 姓名=张三 或者姓名=王五
select * from score where user_name = '张三' or user_name = '王五';
not: 不是
between:范围查询
-- betweem 范围查询 查询 成绩是90-95
select * from score where score between 90 and 95;
not between :不包含这个范围
-- not between查询不在 90-95之间
select * from score where score not between 90 and 95;
3.模糊查询
通配符
- %:任意字符串
- _:任意单个字符
- []:范围内单个字符
- [^]:范围外的单个字符
%:任意字符
-- % 匹配任意字符
-- 获取姓名以王开头的
select * from score where user_name like '王%';
-- 获取中间五的姓名
select * from score where user_name like '%五%';
单个字符
-- 获取x三开的名字
select * from score where user_name like '_三';
3.NULL值
select * from score where gender is null; -- 查询为null
select * from score where gender is not null; -- 查询不为null
4.获取前几条数据 top
select top 2 * from score;-- 获取表中的前两条数据
5.排序(order by)
- 升序 asc
- 降序 desc
select * from score order by score desc; -- 降序
select * from score order by score asc; -- 升序