SELECT
SELECT [DISTINCT] *|{column1, column2, column3..}
FROM tablename;
基础检索
实例:首先创建这张表
create TABLE student(
id int not null DEFAULT 1,
`name` VARCHAR(20) not null DEFAULT '',
chinese FLOAT not null DEFAULT 0.0,
english FLOAT not null DEFAULT 0.0,
math FLOAT not null DEFAULT 0.0
);
INSERT INTO student (id,name,chinese,english,math) VALUES (1,'韩顺平',89,78,90);
INSERT INTO student (id,name,chinese,english,math) VALUES (2,'张飞',67,98,56);
INSERT INTO student (id,name,chinese,english,math) VALUES (3,'宋江',87,78,77);
INSERT INTO student (id,name,chinese,english,math) VALUES (4,'关羽',88,98,90);
INSERT INTO student (id,name,chinese,english,math) VALUES (5,'赵云',82,84,67);
INSERT INTO student (id,name,chinese,english,math) VALUES (6,'欧阳锋',55,85,45);
INSERT INTO student (id,name,chinese,english,math) VALUES (7,'黄蓉',75,65,30);
eg.
-- 查询表中所有学生的信息
select * from student;
-- 查询表中所有学生的姓名和对应的英语成绩
select `name`,english from student;
-- 过滤表中重复数据
select distinct english from student;
要查询的记录,每个字段都相同,才会去重
取别名
SELECT column_name as 别名 from 表名;
eg.
-- 统计各个学生的总分
SELECT `name`,(chinese+english+math) from student;
-- 所有学生总分加十分的情况
SELECT `name`,(chinese+english+math) from student;
-- 使用别名表示学生分数
SELECT `name` as '名字',(chinese+english+math) as total_score from student;
去重
SELECT DISDINCT `字段列表` from `表名`;
条件查询
运算符
比较运算符 | 解释说明 |
---|---|
> | 大于 |
>= | 大于等于 |
< | 小于 |
<= | 小于等于 |
= | 等于 |
<>或!= | 不等于 |
BETWEEN…AND… | 显示在某一区间的值(左右都包含) |
IN (set) | 显示在in列表中的值,例如:in (100,200) |
LIKE ‘张%’ NOT LIKE ‘’ | 模糊查询(_匹配单个字符,%匹配任意个字符) |
IS NULL | 判断是否为空 |
逻辑运算符 | 解释说明 |
---|---|
and | 多个条件同时成立 |
or | 多个条件任一成立 |
not | 不成立,例:where not(salary>100) |
eg.
-- 查询姓名为赵云的学生成绩
select * from student
where `name` = '赵云';
-- 查询英语成绩大于90分的同学
select * from student
where english > 90;
--查询总分大于200分的同学
select * from student
where (chinese+english+math) > 200;
-- 查询math大于70并且id大于90的学生成绩
select * from student
where math>60 and id>90;
-- 查询英语成绩大于语文成绩的同学
select * from student
where english>chinese;
-- 查询总分大于200分并且数学成绩小于语文成绩的姓赵的学生
select * from student
where (chinese+english+math) > 200 and
math < chinese and
`name` like '赵%';
ex.
-- 查询英语分数在80-90之间的同学
select * from student
where english between 80 and 90;
-- 查询数学分数为89,90,91的同学
select * from student
where math in (89,90,91);
-- 查询所有姓李的学生成绩
select * from student
where `name` like '李%';
-- 查询数学分>80,语文分>80的同学
select * from student
where math>80 and chinese>80;
分组查询
聚合函数
排序查询
order by子句
SELECT column1,column2,column3..
FROM table;
order by column4 asc|desc,column5 asc|desc, ...
- Order by 指定排序的列,排序的列既可以是表中的列名,也可以是select语句后指定的列名。
- Asc升序[默认],Desc降序
- ORDER BY子句应位于SELECT语句的结尾
ex.
-- 对数学成绩排序后输出【升序】
select * from student
order by math;
-- 对总分按从高到低的顺序输出
select `name`,(chinese+english+math) as total_score from student
order by (chinese+english+math) desc;
-- 对姓李的学生成绩排序输出(升序)
select * from student
where `name` like '韩%'
order by (chinese+english+math);
分页查询
select 字段列表 from 表名 limit 起始索引,查询记录数;
查询记录数=(查询页码-1)*每页记录数
-- 每页五条数据,查询第一页
SELECT * FROM 表 limit 0,5;
-- 每页五条数据,查询第二页
SELECT * FROM 表 limit 5,5;
-- 每页五条数据,查询第三页
SELECT * FROM 表 limit 10,5;