mysql> SELECT * FROM teacher_class limit 1; +----+--------+--------+-----------+------+------+------------+------------+ | id | t_name | gender | c_name | room | days | begin_date | end_date | +----+--------+--------+-----------+------+------+------------+------------+ | 1 | Gene | male | python001 | 334 | 22 | 2013-03-31 | 2013-05-05 | +----+--------+--------+-----------+------+------+------------+------------+ 1 row in set (0.00 sec)
Limit offset, row_count # offset 偏移量,從0開始; # row_count,總記錄數,長度,不是開始和結束, 若是數量大於餘下的記錄數,則獲取全部餘下的記錄數,默認值0
mysql> SELECT * FROM teacher_class limit 3,4; +----+---------+--------+-----------+------+------+------------+------------+ | id | t_name | gender | c_name | room | days | begin_date | end_date | +----+---------+--------+-----------+------+------+------------+------------+ | 4 | Emma | female | python004 | 338 | 20 | 2013-03-15 | 2013-04-05 | | 5 | Janice | female | python007 | 332 | 27 | 2013-04-15 | 2013-05-10 | | 6 | Jessica | female | python008 | 332 | 28 | 2013-05-15 | 2013-06-08 | +----+---------+--------+-----------+------+------+------------+------------+ 3 rows in set (0.00 sec)
mysql> SELECT days FROM teacher_class; +------+ | days | +------+ | 22 | | 22 | | 55 | | 20 | | 27 | | 28 | +------+ 6 rows in set (0.00 sec) mysql> SELECT DISTINCT days FROM teacher_class; +------+ | days | +------+ | 22 | | 55 | | 20 | | 27 | | 28 | +------+ 5 rows in set (0.00 sec) mysql> SELECT days, begin_date FROM teacher_class; +------+------------+ | days | begin_date | +------+------------+ | 22 | 2013-03-31 | | 22 | 2013-05-31 | | 55 | 2013-02-15 | | 20 | 2013-03-15 | | 27 | 2013-04-15 | | 28 | 2013-05-15 | +------+------------+ 6 rows in set (0.01 sec) mysql> SELECT DISTINCT days, begin_date FROM teacher_class; +------+------------+ | days | begin_date | +------+------------+ | 22 | 2013-03-31 | | 22 | 2013-05-31 | | 55 | 2013-02-15 | | 20 | 2013-03-15 | | 27 | 2013-04-15 | | 28 | 2013-05-15 | +------+------------+ 6 rows in set (0.00 sec)
將多條select語句的結果,合併到一塊兒,稱爲聯合python
使用union關鍵字,聯合兩個select語句便可mysql
注意 若是union查詢結果有重複,會自動去掉重複的數據;若是想獲得所有數據,經過UNION ALL
來顯示數據sql
多個select語句檢索時,字段要一致; 數據類型,儘可能保持一致;若是不一致,應該能讓MySQL可以作類型轉換學習
檢索結果列名稱的問題:根據第一條select語句的字段,進行名稱排列code
mysql> SELECT t_name, days FROM teacher_class WHERE c_name = "python002" ORDER BY days DESC LIMIT 1; +---------+------+ | t_name | days | +---------+------+ | Jessica | 55 | +---------+------+ 1 row in set (0.00 sec) mysql> mysql> SELECT t_name, days FROM teacher_class WHERE c_name = "python008" ORDER BY days DESC LIMIT 1; +---------+------+ | t_name | days | +---------+------+ | Jessica | 28 | +---------+------+ 1 row in set (0.00 sec) # 聯合查詢語法 mysql>(SELECT t_name, days FROM teacher_class WHERE c_name = "python002" ORDER BY days DESC LIMIT 1) UNION (SELECT t_name, days FROM teacher_class WHERE c_name = "python008" ORDER BY days DESC LIMIT 1); +---------+------+ | t_name | days | +---------+------+ | Jessica | 55 | | Jessica | 28 | +---------+------+ 2 rows in set (0.00 sec)
ORDER BY
進行排序mysql> (SELECT t_name, days FROM teacher_class WHERE c_name = "python002" ) \ -> UNION (SELECT t_name, days FROM teacher_class WHERE c_name = "python008" ) ORDER BY days; +---------+------+ | t_name | days | +---------+------+ | Mona | 22 | | Emma | 28 | | Jessica | 28 | | Mona | 55 | | Jessica | 55 | +---------+------+ 5 rows in set (0.00 sec)