所在位置:

mysql的单表查询

mysql的单表查询虽然不算复杂,但查询的条件还是挺多的,在这里做一个单表查询的总结,方便以后来查看〜〜,下面是常用的单表查询:

查询语句

SELECT 字段1, 字段2 FROM 表名 [条件]
[GROUP BY 要分组的字段]
[HAVING 分组后要过滤的条件]
[ORDER BY 字段 ASC|DESC]
[LIMIT 数量];

注意:上面这个语句是单表查询的完整语句,[] 代表可选,| 代表从几个中选择一个

创建表和测试数据

DROP TABLE IF EXISTS `student`;

CREATE TABLE `student` (
  `id` int(3) NOT NULL AUTO_INCREMENT,
  `name` varchar(64) NOT NULL,
  `age` int(3) NOT NULL,
  `sex` varchar(2) NOT NULL DEFAULT '',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO student(name, age, sex) VALUES('xiaoming', 18, '男');
INSERT INTO student(name, age, sex) VALUES('jenny', 28, '女');
INSERT INTO student(name, age, sex) VALUES('jim', 21, '女');
INSERT INTO student(name, age, sex) VALUES('tom', 20, '男');
INSERT INTO student(name, age, sex) VALUES('xiaojun', 18, '男');
INSERT INTO student(name, age, sex) VALUES('bety', 19, '女');
INSERT INTO student(name, age, sex) VALUES('keny', 22, '男');
INSERT INTO student(name, age, sex) VALUES('skyin', 23, '男');
INSERT INTO student(name, age, sex) VALUES('tomminy', 22, '男');
INSERT INTO student(name, age, sex) VALUES('ketty', 21, '女');
INSERT INTO student(name, age, sex) VALUES('jimmy', 18, '男');

查询数据

查询所有字段
SELECT * FROM 表名;

举例:

SELECT * FROM student;

返回结果:

+----+----------+-----+-----+
| id | name     | age | sex |
+----+----------+-----+-----+
|  1 | xiaoming |  18 ||
|  2 | jenny    |  28 ||
|  3 | jim      |  21 ||
|  4 | tom      |  20 ||
|  5 | xiaojun  |  18 ||
|  6 | bety     |  19 ||
|  7 | keny     |  22 ||
|  8 | skyin    |  23 ||
|  9 | tomminy  |  22 ||
| 10 | ketty    |  21 ||
| 11 | jimmy    |  18 ||
+----+----------+-----+-----+
查询指定的字段
SELECT 字段1, 字段2 FROM 表名;

举例:

SELECT name, sex FROM student;

返回结果:

+----------+-----+
| name     | sex |
+----------+-----+
| xiaoming ||
| jenny    ||
| jim      ||
| tom      ||
| xiaojun  ||
| bety     ||
| keny     ||
| skyin    ||
| tomminy  ||
| ketty    ||
| jimmy    ||
+----------+-----+

查询结果排序

查询结果单列排序
SELECT * FROM 表名 [条件] ORDER BY 字段 [ASC|DESC]

举例:

SELECT * FROM student ORDER BY id DESC;

返回结果:

+----+----------+-----+-----+
| id | name     | age | sex |
+----+----------+-----+-----+
| 11 | jimmy    |  18 ||
| 10 | ketty    |  21 ||
|  9 | tomminy  |  22 ||
|  8 | skyin    |  23 ||
|  7 | keny     |  22 ||
|  6 | bety     |  19 ||
|  5 | xiaojun  |  18 ||
|  4 | tom      |  20 ||
|  3 | jim      |  21 ||
|  2 | jenny    |  28 ||
|  1 | xiaoming |  18 ||
+----+----------+-----+-----+
查询结果多列排序
SELECT * FROM 表名 [条件] ORDER BY 字段1, 字段2 [ASC|DESC]

举例:

SELECT * FROM student ORDER BY age, name;

返回结果:

+----+----------+-----+-----+
| id | name     | age | sex |
+----+----------+-----+-----+
| 11 | jimmy    |  18 ||
|  5 | xiaojun  |  18 ||
|  1 | xiaoming |  18 ||
|  6 | bety     |  19 ||
|  4 | tom      |  20 ||
|  3 | jim      |  21 ||
| 10 | ketty    |  21 ||
|  7 | keny     |  22 ||
|  9 | tomminy  |  22 ||
|  8 | skyin    |  23 ||
|  2 | jenny    |  28 ||
+----+----------+-----+-----+

注意: ASC 的排序是从小到大,DESC 的排序是从大到小

查询结果过滤

查询匹配字符的记录
SELECT * FROM 表名 WHERE 字段 LIKE '字段值'; #等于 SELECT * FROM 表名 WHERE 字段='字段值';

举例:

SELECT * FROM student WHERE name like 'tom';

返回结果:

+----+------+-----+-----+
| id | name | age | sex |
+----+------+-----+-----+
|  4 | tom  |  20 ||
+----+------+-----+-----+
查询模糊匹配字符的记录
SELECT * FROM 表名 WHERE 字段 LIKE '%字段值_';

举例:

SELECT * FROM student WHERE name like '%tom%';

返回结果:

+----+---------+-----+-----+
| id | name    | age | sex |
+----+---------+-----+-----+
|  4 | tom     |  20 ||
|  9 | tomminy |  22 ||
+----+---------+-----+-----+

说明:模糊匹配可以使用 % 或者 _% 表示任意多个字符, _表示任意一个字符,可以使用多个 _

查询指定记录
SELECT * FROM 表名 WHERE 字段=字段值;

举例:

SELECT * FROM student WHERE age=18;

返回结果:

+----+----------+-----+-----+
| id | name     | age | sex |
+----+----------+-----+-----+
|  1 | xiaoming |  18 ||
|  5 | xiaojun  |  18 ||
| 11 | jimmy    |  18 ||
+----+----------+-----+-----+

注意:除了 = 的操作符,还有下面的操作符:

  1. <> 不等于

  2. != 不等于

  3. > 大于

  4. < 小于

  5. >= 大于等于

  6. <= 于小等于

  7. IS NULL 不为NULL

带not关键字查询
SELECT * FROM 表名 WHERE NOT 条件;

举例:

SELECT * FROM student WHERE NOT age=18;

返回结果:

+----+---------+-----+-----+
| id | name    | age | sex |
+----+---------+-----+-----+
|  2 | jenny   |  28 ||
|  3 | jim     |  21 ||
|  4 | tom     |  20 ||
|  6 | bety    |  19 ||
|  7 | keny    |  22 ||
|  8 | skyin   |  23 ||
|  9 | tomminy |  22 ||
| 10 | ketty   |  21 ||
+----+---------+-----+-----+
带and关键字查询
SELECT * FROM 表名 WHERE 条件1 AND 条件2;
SELECT * FROM 表名 WHERE NOT (条件1 AND 条件2); # 等于 SELECT * FROM 表名 WHERE (NOT 条件1) OR (NOT 条件2);

举例:

SELECT * FROM student WHERE age>20 AND sex='男';

返回结果:

+----+---------+-----+-----+
| id | name    | age | sex |
+----+---------+-----+-----+
|  7 | keny    |  22 ||
|  8 | skyin   |  23 ||
|  9 | tomminy |  22 ||
+----+---------+-----+-----+
带or关键字查询
SELECT * FROM 表名 WHERE 条件1 OR 条件2;
SELECT * FROM 表名 WHERE NOT (条件1 OR 条件2); # 等于 SELECT * FROM 表名 WHERE (NOT 条件1) AND (NOT 条件2);

举例:

SELECT * FROM student WHERE (age<20 AND sex='男') OR sex='女';

返回结果:

+----+----------+-----+-----+
| id | name     | age | sex |
+----+----------+-----+-----+
|  1 | xiaoming |  18 ||
|  2 | jenny    |  28 ||
|  3 | jim      |  21 ||
|  5 | xiaojun  |  18 ||
|  6 | bety     |  19 ||
| 10 | ketty    |  21 ||
| 11 | jimmy    |  18 ||
+----+----------+-----+-----+

注意:notandor 一起使用时,优先级从大到小:not > and > or,如果这三个有两上以上一起使用,建议使用圆括号来改变他们的优先级

使用 between 查询指定范围记录
SELECT * FROM 表名 WHERE 字段 BETWEEN 字段值1 AND 字段值2;

举例:

SELECT * FROM student WHERE age BETWEEN 18 AND 20;

返回结果:

+----+----------+-----+-----+
| id | name     | age | sex |
+----+----------+-----+-----+
|  1 | xiaoming |  18 ||
|  4 | tom      |  20 ||
|  5 | xiaojun  |  18 ||
|  6 | bety     |  19 ||
| 11 | jimmy    |  18 ||
+----+----------+-----+-----+
带in关键字查询
SELECT * FROM 表名 WHERE 字段 IN (字段值1,字段值2);

举例:

SELECT * FROM student WHERE age IN (18, 20);

返回结果:

+----+----------+-----+-----+
| id | name     | age | sex |
+----+----------+-----+-----+
|  1 | xiaoming |  18 ||
|  4 | tom      |  20 ||
|  5 | xiaojun  |  18 ||
| 11 | jimmy    |  18 ||
+----+----------+-----+-----+

注意: BETWEEN AND 是一个范围来的,而 IN 一定是在圆括号中的某个值

聚集函数

COUNT 函数用来返回某列的总行数
SELECT COUNT(*) FROM 表名 [条件];

举例:

SELECT COUNT(*) as total FROM student;

返回结果:

+-------+
| total |
+-------+
|    11 |
+-------+

注意:,如果某列有 NULL 值,COUNT(*)COUNT(字段)的返回值是不同的,COUNT(*) 的返回值是所有行的总数, 而 COUNT(字段) 的返回值是不包含 NULL 值的总数

SUM 函数用来返回某列的总和
SELECT SUM(字段) FROM 表名 [条件];

举例:

SELECT SUM(age) as sum_age FROM student;

返回结果:

+---------+
| sum_age |
+---------+
|     230 |
+---------+
AVG 函数用来返回某列的平均数
Select AVG(字段) from 表名 [条件];

举例:

SELECT AVG(age) as avg_age FROM student;

返回结果:

+---------+
| avg_age |
+---------+
| 20.9091 |
+---------+
MAX 函数用来返回某列的最大值
Select MAX(字段) from 表名 [条件];

举例:

SELECT MAX(age) as max_age FROM student;

返回结果:

+---------+
| max_age |
+---------+
|      28 |
+---------+
MIN 函数用来返回某列的最小值
Select MIN(字段) from 表名 [条件];

举例:

SELECT MIN(age) as max_age FROM student;

返回结果:

+---------+
| min_age |
+---------+
|      18 |
+---------+

查询结果不重复

SELECT DISTINCT 字段 FROM 表名;

举例:

SELECT DISTINCT sex from student;

返回结果:

+-----+
| sex |
+-----+
||
||
+-----+

注意: DISTINCT 后面只能加一个字段

分组查询

SELECT 字段[, 聚集函数] FROM 表名 [条件] GROUP BY 字段;

举例:

SELECT sex, AVG(age) AS avg_age FROM student WHERE age > 20 GROUP BY sex;

返回结果:

+-----+---------+
| sex | avg_age |
+-----+---------+
|| 23.3333 |
|| 22.3333 |
+-----+---------+

注意: 分组的意思就是把相同的字段分成一组

使用 HAVING 过滤分组

SELECT 字段 FROM 表名 [条件] GROUP BY 字段 HAVING 条件;

举例:

SELECT sex, COUNT(*) FROM student WHERE age > 19 GROUP BY sex HAVING COUNT(*) > 3;

返回结果:

+-----+----------+
| sex | COUNT(*) |
+-----+----------+
||        4 |
+-----+----------+

注意: WHEREHAVING 都是过滤数据的,但是 WHERE 主要过滤列的,而 HAVING 主要过滤分组后的数据的

限制查询结果行数量

SELECT * FROM 表名 LIMIT [条数|从第几条,共几条];

举例:

SELECT * FROM student WHERE name LIKE '%t%' ORDER BY age DESC limit 1, 2;

返回结果:

+----+-------+-----+-----+
| id | name  | age | sex |
+----+-------+-----+-----+
| 10 | ketty |  21 ||
|  4 | tom   |  20 ||
+----+-------+-----+-----+

【上一篇】Mac的窗口管理工具

【下一篇】mysql的多表查询