赞
踩
SELECT [DISTINCT] * | {column [, column] ...} [FROM table_name]
[WHERE ...] [ORDER BY column [ASC | DESC], ...] LIMIT ...
// 创建表结构 CREATE TABLE exam_result ( id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT, name VARCHAR(20) NOT NULL COMMENT '同学姓名', chinese float DEFAULT 0.0 COMMENT '语文成绩', math float DEFAULT 0.0 COMMENT '数学成绩', english float DEFAULT 0.0 COMMENT '英语成绩' ); // 插入测试数据 INSERT INTO exam_result (name, chinese, math, english) VALUES ('唐三藏', 67, 98, 56), ('孙悟空', 87, 78, 77), ('猪悟能', 88, 98, 90), ('曹孟德', 82, 84, 67), ('刘玄德', 55, 85, 45), ('孙权', 70, 73, 78), ('宋公明', 75, 65, 30); Query OK, 7 rows affected (0.00 sec) Records: 7 Duplicates: 0 Warnings: 0
SELECT * FROM exam_result;
+----+-----------+---------+------+---------+
| id | name | chinese | math | english |
+----+-----------+---------+------+---------+
| 1 | 唐三藏 | 67 | 98 | 56 |
| 2 | 孙悟空 | 87 | 78 | 77 |
| 3 | 猪悟能 | 88 | 98 | 90 |
| 4 | 曹孟德 | 82 | 84 | 67 |
| 5 | 刘玄德 | 55 | 85 | 45 |
| 6 | 孙权 | 70 | 73 | 78 |
| 7 | 宋公明 | 75 | 65 | 30 |
+----+-----------+---------+------+---------+
7 rows in set (0.00 sec)
SELECT id, name, 10 FROM exam_result;
+----+-----------+----+
| id | name | 10 |
+----+-----------+----+
| 1 | 唐三藏 | 10 |
| 2 | 孙悟空 | 10 |
| 3 | 猪悟能 | 10 |
| 4 | 曹孟德 | 10 |
| 5 | 刘玄德 | 10 |
| 6 | 孙权 | 10 |
| 7 | 宋公明 | 10 |
+----+-----------+----+
7 rows in set (0.00 sec)
SELECT id, name, english + 10 FROM exam_result;
+----+-----------+--------------+
| id | name | english + 10 |
+----+-----------+--------------+
| 1 | 唐三藏 | 66 |
| 2 | 孙悟空 | 87 |
| 3 | 猪悟能 | 100 |
| 4 | 曹孟德 | 77 |
| 5 | 刘玄德 | 55 |
| 6 | 孙权 | 88 |
| 7 | 宋公明 | 40 |
+----+-----------+--------------+
SELECT id, name, chinese + math + english FROM exam_result;
+----+--------- -+--------------------------+
| id | name | chinese + math + english |
+----+-----------+--------------------------+
| 1 | 唐三藏 | 221 |
| 2 | 孙悟空 | 242 |
| 3 | 猪悟能 | 276 |
| 4 | 曹孟德 | 233 |
| 5 | 刘玄德 | 185 |
| 6 | 孙权 | 221 |
| 7 | 宋公明 | 170 |
+----+-----------+--------------------------+
7 rows in set (0.00 sec)
SELECT column [AS] alias_name [...] FROM table_name;
SELECT id, name, chinese + math + english 总分 FROM exam_result;
+----+-----------+--------+
| id | name | 总分 |
+----+-----------+--------+
| 1 | 唐三藏 | 221 |
| 2 | 孙悟空 | 242 |
| 3 | 猪悟能 | 276 |
| 4 | 曹孟德 | 233 |
| 5 | 刘玄德 | 185 |
| 6 | 孙权 | 221 |
| 7 | 宋公明 | 170 |
+----+-----------+--------+
7 rows in set (0.00 sec)
// 98分重复了 SELECT math FROM exam_result; +--------+ | math | +--------+ | 98 | | 78 | | 98 | | 84 | | 85 | | 73 | | 65 | +--------+ 7 rows in set (0.00 sec) // 去重结果 SELECT DISTINCT math FROM exam_result; +--------+ | math | +--------+ | 98 | | 78 | | 84 | | 85 | | 73 | | 65 | +--------+ 6 rows in set (0.00 sec)
SELECT name, english FROM exam_result WHERE english < 60;
+-----------+---------+
| name | english |
+-----------+---------+
| 唐三藏 | 56 |
| 刘玄德 | 45 |
| 宋公明 | 30 |
+-----------+---------+
3 rows in set (0.01 sec)
# 使用 AND 进行条件连接 SELECT name, chinese FROM exam_result WHERE chinese >= 80 AND chinese <= 90; +-----------+---------+ | name | chinese | +-----------+---------+ | 孙悟空 | 87 | | 猪悟能 | 88 | | 曹孟德 | 82 | +-----------+---------+ 3 rows in set (0.00 sec) # 使用 BETWEEN ... AND ... 条件 SELECT name, chinese FROM exam_result WHERE chinese BETWEEN 80 AND 90; +-----------+---------+ | name | chinese | +-----------+---------+ | 孙悟空 | 87 | | 猪悟能 | 88 | | 曹孟德 | 82 | +-----------+---------+ 3 rows in set (0.00 sec)
# 使用 OR 进行条件连接 SELECT name, math FROM exam_result WHERE math = 58 OR math = 59 OR math = 98 OR math = 99; +-----------+--------+ | name | math | +-----------+--------+ | 唐三藏 | 98 | | 猪悟能 | 98 | +-----------+--------+ 2 rows in set (0.01 sec) # 使用 IN 条件 SELECT name, math FROM exam_result WHERE math IN (58, 59, 98, 99); +-----------+--------+ | name | math | +-----------+--------+ | 唐三藏 | 98 | | 猪悟能 | 98 | +-----------+--------+ 2 rows in set (0.00 sec)
# % 匹配任意多个(包括 0 个)任意字符 SELECT name FROM exam_result WHERE name LIKE '孙%'; +-----------+ | name | +-----------+ | 孙悟空 | | 孙权 | +-----------+ 2 rows in set (0.00 sec) # _ 匹配严格的一个任意字符 SELECT name FROM exam_result WHERE name LIKE '孙_'; +--------+ | name | +--------+ | 孙权 | +--------+ 1 row in set (0.00 sec)
# WHERE 条件中比较运算符两侧都是字段
SELECT name, chinese, english FROM exam_result WHERE chinese > english;
+-----------+---------+---------+
| name | chinese | english |
+-----------+---------+---------+
| 唐三藏 | 67 | 56 |
| 孙悟空 | 87 | 77 |
| 曹孟德 | 82 | 67 |
| 刘玄德 | 55 | 45 |
| 宋公明 | 75 | 30 |
+-----------+---------+---------+
5 rows in set (0.00 sec)
# WHERE 条件中使用表达式
# 别名不能用在 WHERE 条件中
SELECT name, chinese + math + english 总分 FROM exam_result WHERE chinese + math + english < 200;
+-----------+--------+
| name | 总分 |
+-----------+--------+
| 刘玄德 | 185 |
| 宋公明 | 170 |
+-----------+--------+
2 rows in set (0.00 sec)
# AND 与 NOT 的使用
SELECT name, chinese FROM exam_result WHERE chinese > 80 AND name NOT LIKE '孙%';
+----+-----------+---------+--------+---------+
| id | name | chinese | math | english |
+----+-----------+---------+--------+---------+
| 3 | 猪悟能 | 88 | 98 | 90 |
| 4 | 曹孟德 | 82 | 84 | 67 |
+----+-----------+---------+--------+---------+
2 rows in set (0.00 sec)
# 综合性查询,括号()内的可以看成一整个条件
SELECT name, chinese, math, english, chinese + math + english 总分 FROM exam_result
WHERE name LIKE '孙_' OR (chinese + math + english > 200 AND chinese < math AND english >80);
+-----------+---------+--------+---------+--- ---+
| name | chinese | math | english | 总分 |
+-----------+---------+--------+---------+-------+
| 猪悟能 | 88 | 98 | 90 | 276 |
| 孙权 | 70 | 73 | 78 | 221 |
+-----------+---------+--------+---------+-------+
2 rows in set (0.00 sec)
# 查询 students 表 +-----+-------+-----------+-------+ | id | sn | name | qq | +-----+-------+-----------+-------+ | 100 | 10010 | 唐大师 | NULL | | 101 | 10001 | 孙悟空 | 11111 | | 103 | 20002 | 孙仲谋 | NULL | | 104 | 20001 | 曹阿瞒 | NULL | +-----+-------+-----------+-------+ 4 rows in set (0.00 sec) # 查询 qq 号已知的同学姓名 SELECT name, qq FROM students WHERE qq IS NOT NULL; +-----------+-------+ | name | qq | +-----------+-------+ | 孙悟空 | 11111 | +-----------+-------+ 1 row in set (0.00 sec) # NULL 和 NULL 的比较,= 和 <=> 的区别 SELECT NULL = NULL, NULL = 1, NULL = 0; +-------------+----------+----------+ | NULL = NULL | NULL = 1 | NULL = 0 | +-------------+----------+----------+ | NULL | NULL | NULL | +-------------+----------+----------+ 1 row in set (0.00 sec) SELECT NULL <=> NULL, NULL <=> 1, NULL <=> 0; +---------------+------------+------------+ | NULL <=> NULL | NULL <=> 1 | NULL <=> 0 | +---------------+------------+------------+ | 1 | 0 | 0 | +---------------+------------+------------+ 1 row in set (0.00 sec)
-- ASC 为升序(从小到大)
-- DESC 为降序(从大到小)
-- 默认为 ASC
SELECT ... FROM table_name [WHERE ...]
ORDER BY column [ASC|DESC], [...];
SELECT name, math FROM exam_result ORDER BY math;
+-----------+--------+
| name | math |
+-----------+--------+
| 宋公明 | 65 |
| 孙权 | 73 |
| 孙悟空 | 78 |
| 曹孟德 | 84 |
| 刘玄德 | 85 |
| 唐三藏 | 98 |
| 猪悟能 | 98 |
+-----------+--------+
7 rows in set (0.00 sec)
# NULL 视为比任何值都小,升序出现在最上面 SELECT name, qq FROM students ORDER BY qq; +-----------+-------+ | name | qq | +-----------+-------+ | 唐大师 | NULL | | 孙仲谋 | NULL | | 曹阿瞒 | NULL | | 孙悟空 | 11111 | +-----------+-------+ 4 rows in set (0.00 sec) # NULL 视为比任何值都小,降序出现在最下面 SELECT name, qq FROM students ORDER BY qq DESC; +-----------+-------+ | name | qq | +-----------+-------+ | 孙悟空 | 11111 | | 唐大师 | NULL | | 孙仲谋 | NULL | | 曹阿瞒 | NULL | +-----------+-------+ 4 rows in set (0.00 sec)
# 多字段排序,排序优先级随书写顺序
SELECT name, math, english, chinese FROM exam_result ORDER BY math DESC, english, chinese;
+-----------+--------+---------+---------+
| name | math | english | chinese |
+-----------+--------+---------+---------+
| 唐三藏 | 98 | 56 | 67 |
| 猪悟能 | 98 | 90 | 88 |
| 刘玄德 | 85 | 45 | 55 |
| 曹孟德 | 84 | 67 | 82 |
| 孙悟空 | 78 | 77 | 87 |
| 孙权 | 73 | 78 | 70 |
| 宋公明 | 65 | 30 | 75 |
+-----------+--------+---------+---------+
7 rows in set (0.00 sec)
# ORDER BY 中可以使用表达式 SELECT name, chinese + english + math FROM exam_result ORDER BY chinese + english + math DESC; +-----------+--------------------------+ | name | chinese + english + math | +-----------+--------------------------+ | 猪悟能 | 276 | | 孙悟空 | 242 | | 曹孟德 | 233 | | 唐三藏 | 221 | | 孙权 | 221 | | 刘玄德 | 185 | | 宋公明 | 170 | +-----------+--------------------------+ 7 rows in set (0.00 sec) # ORDER BY 子句中可以使用列别名 SELECT name, chinese + english + math 总分 FROM exam_result ORDER BY 总分 DESC; +-----------+--------+ | name | 总分 | +-----------+--------+ | 猪悟能 | 276 | | 孙悟空 | 242 | | 曹孟德 | 233 | | 唐三藏 | 221 | | 孙权 | 221 | | 刘玄德 | 185 | | 宋公明 | 170 | +-----------+--------+ 7 rows in set (0.00 sec)
# 结合 WHERE 子句 和 ORDER BY 子句
SELECT name, math FROM exam_result WHERE name LIKE '孙%' OR name LIKE '曹%' ORDER BY math DESC;
+-----------+--------+
| name | math |
+-----------+--------+
| 曹孟德 | 84 |
| 孙悟空 | 78 |
| 孙权 | 73 |
+-----------+--------+
3 rows in set (0.00 sec)
# 起始下标为 0
# 从 0 开始,筛选 n 条结果
SELECT ... FROM table_name [WHERE ...] [ORDER BY ...] LIMIT n;
# 从 s 开始,筛选 n 条结果
SELECT ... FROM table_name [WHERE ...] [ORDER BY ...] LIMIT s, n;
# 从 s 开始,筛选 n 条结果,比第二种用法更明确,建议使用
SELECT ... FROM table_name [WHERE ...] [ORDER BY ...] LIMIT n OFFSET s;
// 第 1 页 SELECT id, name, math, english, chinese FROM exam_result ORDER BY id LIMIT 3 OFFSET 0; +----+-----------+--------+---------+---------+ | id | name | math | english | chinese | +----+-----------+--------+---------+---------+ | 1 | 唐三藏 | 98 | 56 | 67 | | 2 | 孙悟空 | 78 | 77 | 87 | | 3 | 猪悟能 | 98 | 90 | 88 | +----+-----------+--------+---------+---------+ 3 rows in set (0.02 sec) // 第 2 页 SELECT id, name, math, english, chinese FROM exam_result ORDER BY id LIMIT 3 OFFSET 3; +----+-----------+--------+---------+---------+ | id | name | math | english | chinese | +----+-----------+--------+---------+---------+ | 4 | 曹孟德 | 84 | 67 | 82 | | 5 | 刘玄德 | 85 | 45 | 55 | | 6 | 孙权 | 73 | 78 | 70 | +----+-----------+--------+---------+---------+ 3 rows in set (0.00 sec) // 第 3 页,如果结果不足 3 个,不会有影响 SELECT id, name, math, english, chinese FROM exam_result ORDER BY id LIMIT 3 OFFSET 6; +----+-----------+--------+---------+---------+ | id | name | math | english | chinese | +----+-----------+--------+---------+---------+ | 7 | 宋公明 | 65 | 30 | 75 | +----+-----------+--------+---------+---------+ 1 row in set (0.00 sec)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。