MySQL学习基础之一 — 数据库查询

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

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

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

准备数据

为了便于讲解和练习,咱们先准备好了一个students表和一个classes表,它们的结构和数据以下:浏览器

students表存储了学生信息:spa

id class_id name gender score
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表存储了班级信息:code

id name
1 一班
2 二班
3 三班
4 四班

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

1、基本查询

SELECT * FROM <表名> 查询一个表的全部行和全部列的数据 

SELECT * FROM students;

2、条件查询

SELECT * FROM <表名> WHERE <条件表达式> 经过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';

3、投影查询

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';

4、排序

SELECT id, name, gender, score FROM students WHERE class_id = 1 ORDER BY score DESC;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;
ORDER BY

5、分页

SELECT id, name, gender, score FROM students ORDER BY score DESC LIMIT 3 OFFSET 0;LIMIT <M> OFFSET <N> 结果集中“截取”出第M~N条记录
相关文章
相关标签/搜索