SELECT

  1. SELECT [DISTINCT] *|{column1, column2, column3..}
  2. FROM tablename;

基础检索

实例:首先创建这张表

  1. create TABLE student(
  2. id int not null DEFAULT 1,
  3. `name` VARCHAR(20) not null DEFAULT '',
  4. chinese FLOAT not null DEFAULT 0.0,
  5. english FLOAT not null DEFAULT 0.0,
  6. math FLOAT not null DEFAULT 0.0
  7. );
  8. INSERT INTO student (id,name,chinese,english,math) VALUES (1,'韩顺平',89,78,90);
  9. INSERT INTO student (id,name,chinese,english,math) VALUES (2,'张飞',67,98,56);
  10. INSERT INTO student (id,name,chinese,english,math) VALUES (3,'宋江',87,78,77);
  11. INSERT INTO student (id,name,chinese,english,math) VALUES (4,'关羽',88,98,90);
  12. INSERT INTO student (id,name,chinese,english,math) VALUES (5,'赵云',82,84,67);
  13. INSERT INTO student (id,name,chinese,english,math) VALUES (6,'欧阳锋',55,85,45);
  14. INSERT INTO student (id,name,chinese,english,math) VALUES (7,'黄蓉',75,65,30);

eg.

  1. -- 查询表中所有学生的信息
  2. select * from student;
  3. -- 查询表中所有学生的姓名和对应的英语成绩
  4. select `name`,english from student;
  5. -- 过滤表中重复数据
  6. select distinct english from student;

要查询的记录,每个字段都相同,才会去重

取别名

  1. SELECT column_name as 别名 from 表名;

eg.

  1. -- 统计各个学生的总分
  2. SELECT `name`,(chinese+english+math) from student;
  3. -- 所有学生总分加十分的情况
  4. SELECT `name`,(chinese+english+math) from student;
  5. -- 使用别名表示学生分数
  6. SELECT `name` as '名字',(chinese+english+math) as total_score from student;

去重

  1. SELECT DISDINCT `字段列表` from `表名`;

条件查询

运算符

比较运算符 解释说明
> 大于
>= 大于等于
< 小于
<= 小于等于
= 等于
<>或!= 不等于
BETWEEN…AND… 显示在某一区间的值(左右都包含)
IN (set) 显示在in列表中的值,例如:in (100,200)
LIKE ‘张%’ NOT LIKE ‘’ 模糊查询(_匹配单个字符,%匹配任意个字符)
IS NULL 判断是否为空
逻辑运算符 解释说明
and 多个条件同时成立
or 多个条件任一成立
not 不成立,例:where not(salary>100)

eg.

  1. -- 查询姓名为赵云的学生成绩
  2. select * from student
  3. where `name` = '赵云';
  4. -- 查询英语成绩大于90分的同学
  5. select * from student
  6. where english > 90;
  7. --查询总分大于200分的同学
  8. select * from student
  9. where (chinese+english+math) > 200;
  10. -- 查询math大于70并且id大于90的学生成绩
  11. select * from student
  12. where math>60 and id>90;
  13. -- 查询英语成绩大于语文成绩的同学
  14. select * from student
  15. where english>chinese;
  16. -- 查询总分大于200分并且数学成绩小于语文成绩的姓赵的学生
  17. select * from student
  18. where (chinese+english+math) > 200 and
  19. math < chinese and
  20. `name` like '赵%';

ex.

  1. -- 查询英语分数在80-90之间的同学
  2. select * from student
  3. where english between 80 and 90;
  4. -- 查询数学分数为89,90,91的同学
  5. select * from student
  6. where math in (89,90,91);
  7. -- 查询所有姓李的学生成绩
  8. select * from student
  9. where `name` like '李%';
  10. -- 查询数学分>80,语文分>80的同学
  11. select * from student
  12. where math>80 and chinese>80;

分组查询

聚合函数

排序查询

order by子句

  1. SELECT column1,column2,column3..
  2. FROM table;
  3. order by column4 asc|desc,column5 asc|desc, ...
  1. Order by 指定排序的列,排序的列既可以是表中的列名,也可以是select语句后指定的列名。
  2. Asc升序[默认],Desc降序
  3. ORDER BY子句应位于SELECT语句的结尾

ex.

  1. -- 对数学成绩排序后输出【升序】
  2. select * from student
  3. order by math;
  4. -- 对总分按从高到低的顺序输出
  5. select `name`,(chinese+english+math) as total_score from student
  6. order by (chinese+english+math) desc;
  7. -- 对姓李的学生成绩排序输出(升序)
  8. select * from student
  9. where `name` like '韩%'
  10. order by (chinese+english+math);

分页查询

select 字段列表 from 表名 limit 起始索引,查询记录数;

查询记录数=(查询页码-1)*每页记录数

  1. -- 每页五条数据,查询第一页
  2. SELECT * FROM limit 0,5;
  3. -- 每页五条数据,查询第二页
  4. SELECT * FROM limit 5,5;
  5. -- 每页五条数据,查询第三页
  6. SELECT * FROM limit 10,5;