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
SELECT * FROM <表名> 查詢一個表的全部行和全部列的數據
SELECT * FROM students;
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';
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';
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
SELECT id, name, gender, score FROM students ORDER BY score DESC LIMIT 3 OFFSET 0;LIMIT <M> OFFSET <N> 結果集中「截取」出第M~N條記錄