mysql入門_條件查詢、排序查詢
阿新 • • 發佈:2021-09-04
mysql 條件查詢 排序查詢
排序查詢的關鍵詞為order by 列名1,列名2... desc/asc desc:按照指定的列降序排列,asc:按照指定列升序排列(預設動作),order by通常在查詢語句最末尾。
排序查詢一般和限定行數關鍵詞limit同時使用,當同時使用時在sql語句中的先後順序為where 限定條件 order by 排序 limit n限定輸出行數。
語法:select * from table_name order by column_name1,column_name2 desc/asc;
條件查詢
條件查詢分類
- 關係運算:>、<、>=、<=、<>/!=、=
- 邏輯運算:and, or, not
- 其它運算:betwen and, like, in, not in, is null, is not null 等等
用法示例
#查詢成績大於90分的記錄 mysql> SELECT * FROM score where degree > 90; +----+-----+-------+--------+ | id | sno | cno | degree | +----+-----+-------+--------+ | 4 | 103 | 3-105 | 92 | | 8 | 105 | 3-105 | 91 | #查詢3-245班級的成績 mysql> SELECT * FROM score where cno='3-245'; +----+-----+-------+--------+ | id | sno | cno | degree | +----+-----+-------+--------+ | 1 | 103 | 3-245 | 86 | | 2 | 105 | 3-245 | 75 | | 3 | 109 | 3-245 | 68 | #查詢成績在80~90之間的記錄(包含80 90) mysql> SELECT * FROM score where degree between 80 and 90; +----+-----+-------+--------+ | id | sno | cno | degree | +----+-----+-------+--------+ | 1 | 103 | 3-245 | 86 | | 5 | 105 | 3-105 | 88 | #查詢成績在80~90之間的記錄(不含80 90) mysql> SELECT * FROM score where degree > 80 and degree < 90; +----+-----+-------+--------+ | id | sno | cno | degree | +----+-----+-------+--------+ | 1 | 103 | 3-245 | 86 | | 5 | 105 | 3-105 | 88 | #查詢3-245班級後者成績大於90分的記錄 mysql> SELECT * FROM score where cno='3-245' or degree > 90; +----+-----+-------+--------+ | id | sno | cno | degree | +----+-----+-------+--------+ | 1 | 103 | 3-245 | 86 | | 2 | 105 | 3-245 | 75 | | 3 | 109 | 3-245 | 68 | | 4 | 103 | 3-105 | 92 | | 8 | 105 | 3-105 | 91 | #查詢成績是86或者91分的記錄 mysql> SELECT * FROM score where degree in (86, 91); +----+-----+-------+--------+ | id | sno | cno | degree | +----+-----+-------+--------+ | 1 | 103 | 3-245 | 86 | | 8 | 105 | 3-105 | 91 | #查詢不是3-245班的成績記錄 mysql> SELECT * FROM score where cno not in ('3-245'); mysql> SELECT * FROM score where cno<>'3-245'; mysql> SELECT * FROM score where cno!='3-245'; +----+-----+-------+--------+ | id | sno | cno | degree | +----+-----+-------+--------+ | 4 | 103 | 3-105 | 92 | | 5 | 105 | 3-105 | 88 | #查詢成績非空的記錄 mysql> SELECT * FROM score where degree is not null; +----+-----+-------+--------+ | id | sno | cno | degree | +----+-----+-------+--------+ | 1 | 103 | 3-245 | 86 | | 2 | 105 | 3-245 | 75 | #查詢學生姓名以“李”開頭的學生記錄 mysql> SELECT * FROM student where sname like '李%'; +-----+--------+------+---------------------+-------+----------+ | sno | sname | ssex | sbirthday | class | romemate | +-----+--------+------+---------------------+-------+----------+ | 101 | 李軍 | 男 | 1976-02-20 00:00:00 | 95033 | 103 | #查詢學生姓名為兩個字且第二個字是“芳”的學生記錄 mysql> SELECT * FROM student where sname like '_芳'; +-----+--------+------+---------------------+-------+----------+ | sno | sname | ssex | sbirthday | class | romemate | +-----+--------+------+---------------------+-------+----------+ | 109 | 王芳 | 女 | 1975-02-10 00:00:00 | 95031 | 108 | #查詢學生姓名中帶“麗”的學生記錄 mysql> SELECT * FROM student where sname like '%麗%'; +-----+--------+------+---------------------+-------+----------+ | sno | sname | ssex | sbirthday | class | romemate | +-----+--------+------+---------------------+-------+----------+ | 107 | 王麗 | 女 | 1976-01-23 00:00:00 | 95033 | 108 |排序查詢
mysql> select * from score where cno='3-105' order by degree,sno limit 3;
+----+-----+-------+--------+
| id | sno | cno | degree |
+----+-----+-------+--------+
| 7 | 103 | 3-105 | 64 |
| 6 | 109 | 3-105 | 76 |
| 9 | 109 | 3-105 | 78 |