MySQL测试必懂的数据库语句

廖大神的练手神器:在线SQL:

https://www.liaoxuefeng.com/wiki/1177760294764384/1179611432985088

运行MySQL等实际的数据库软件,即可在线编写并执行SQL语句。

 

准备数据

为了便于讲解和练习,我们先准备好了一个 students 和一个 classes ,它们的结构和数据如下:

students 表存储了学生信息:

idclass_idnamegenderscore
1 1 小明 M 90
2 1 小红 F 95
3 1 小军 M 88
4 1 小米 F 73
5 2 小白 F 81
6 2 小兵 M 55
7 2 小林 M 85
8 3 小新 F 91
9 3 小王 M 89
10 3 小丽 F 85

classes 表存储了班级信息:

idname
1 一班
2 二班
3 三班
4 四班

请注意,和 MySQL 的持久化存储不同的是,由于我们使用的是 AlaSQL 内存数据库,两张表的数据在页面加载时导入,并且只存在于浏览器的内存中,因此,刷新页面后,数据会重置为上述初始值。

一、基本查询

SELECT * FROM <表名>   查询一个表的所有行和所有列的数据 
SELECT * FROM students;

二、条件查询

SELECT * FROM<表名> WHERE <条件表达式>    通过WHERE条件来设定查询条件
SELECT * FROM students WHERE score >= 80;
SELECT * FROM students WHERE score >= 80 AND gender = 'M';
SELECT * FROM students WHERE score >= 80 OR gender = 'M';
SELECT * FROM students WHERE NOT class_id = 2;
SELECT * FROM students WHERE (score < 80 OR score > 90) AND gender = 'M';

三、投影查询

 SELECT 列1, 列2, 列3 FROM ...         结果集仅包含指定列,这种操作称为投影查询
SELECT id, score, name FROM students;
SELECT id, score points, name FROM students;
SELECT id, score points, name FROM students WHERE gender = 'M';

四、排序

ORDER BY        使用 ORDER BY 可以对结果集进行排序
SELECT id, name, gender, score FROM students ORDER BY score;
SELECT id, name, gender, score FROM students ORDER BY score DESC;
SELECT id, name, gender, score FROM students WHERE class_id = 1 ORDER BY score DESC;

五、分页

LIMIT  OFFSET        结果集中“截取”出第M~N条记录
SELECT id, name, gender, score FROM students
ORDER BY score DESC LIMIT 3 OFFSET 0;
posted @ 2019-06-21 18:54  牵只蜗牛去散步  阅读(554)  评论(0编辑  收藏  举报