- 首页
- 正文
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 | 男 |
+----+----------+-----+-----+
注意:
除了 =
的操作符,还有下面的操作符:
<>
不等于!=
不等于>
大于<
小于>=
大于等于<=
于小等于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 | 男 |
+----+----------+-----+-----+
注意:
当 not
和 and
和 or
一起使用时,优先级从大到小: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 |
+-----+----------+
注意:
WHERE
和 HAVING
都是过滤数据的,但是 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的多表查询