(一)模糊查詢
- 1、-like:
mysql> select name as "姓名" from score where name like '張%';
+--------+
| 姓名 |
+--------+
| 張三 |
| 張玉潔 |
| 張三 |
| 張三 |
| 張三 |
| 張玉潔 |
| 張玉潔 |
| 張玉潔 |
+--------+
8 rows in set
2、is null
mysql> select name as "姓名" from score where name is null;
Empty set
3、between
mysql> select name , score from score where score between 80 and 100;
+--------+-------+
| name | score |
+--------+-------+
| 張玉潔 | 98 |
| 張三 | 86 |
| 張三 | 97 |
| 張玉潔 | 98 |
| 李一 | 80 |
+--------+-------+
5 rows in set
4、in
mysql> select name,score from score where score in (97,98);
+--------+-------+
| name | score |
+--------+-------+
| 張玉潔 | 98 |
| 張三 | 97 |
| 張玉潔 | 98 |
+--------+-------+
3 rows in set
(二)聚合函數
1、sum()
mysql> select sum(score) as "李一總成績" from score where name="李一";
+------------+
| 李一總成績 |
+------------+
| 223 |
+------------+
1 row in set
2、avg()
mysql> select avg(score) as "李一平均成績" from score where name="李一";
+--------------+
| 李一平均成績 |
+--------------+
| 74.3333 |
+--------------+
1 row in set
3.max() min()
mysql> select max(score) from score;
+------------+
| max(score) |
+------------+
| 98 |
+------------+
1 row in set
4.count()
mysql> select count(score) as "大於70分的成績個數" from score where score > 70;
+--------------------+
| 大於70分的成績個數 |
+--------------------+
| 6 |
+--------------------+
1 row in set
(三)分組查詢:
mysql> select name,avg(score),courseName from score group by name,courseName;
+--------+------------+------------+
| name | avg(score) | courseName |
+--------+------------+------------+
| 張三 | 97.0000 | 數學 |
| 張三 | 67.0000 | 英語 |
| 張三 | 60.0000 | 語文 |
| 張玉潔 | 43.0000 | 數學 |
| 張玉潔 | 98.0000 | 英語 |
| 張玉潔 | 98.0000 | 語文 |
| 李一 | 80.0000 | 數學 |
| 李一 | 66.0000 | 英語 |
| 李一 | 77.0000 | 語文 |
+--------+------------+------------+
mysql> select name,avg(score),courseName from score group by name,courseName having count(courseName)>1;
+--------+------------+------------+
| name | avg(score) | courseName |
+--------+------------+------------+
| 張三 | 60.0000 | 語文 |
| 張玉潔 | 43.0000 | 數學 |
+--------+------------+------------+
2 rows in set
分組查詢總結:
1.WHERE子句從數據源中去掉不符合其搜索條件的數據
2.GROUP BY子句搜集數據行到各個組中
3.統計函數為各個組計算統計值
4.HAVING子句去掉不符合其組搜索條件的各組數據行
5使用GROUP BY時,select后面出現的內容要么為聚合函數,要么為group by后面出現的內容
(四)多表鏈接查詢:
1.內連接:
(1)用inner join 但這種方法不常用 常用第二種;
mysql> select username,starname from users as u inner join star as s on u.starid=s.starid;
+----------+----------+
| username | starname |
+----------+----------+
| 張一 | 白羊座 |
| 李二 | 金牛座 |
| 王三 | 雙子座 |
| 張四 | 巨蟹座 |
| 李五 | 獅子座 |
| 張八 | 處女座 |
| 王九 | 天秤座 |
| 張言 | 天蠍座 |
| 李志 | 射手座 |
| 王月 | 水瓶座 |
| 張欣 | 雙魚座 |
+----------+----------+
11 rows in set
(2)from where
select username,starname from users as u,star s where u.starid=s.starid;
+----------+----------+
| username | starname |
+----------+----------+
| 張一 | 白羊座 |
| 李二 | 金牛座 |
| 王三 | 雙子座 |
| 張四 | 巨蟹座 |
| 李五 | 獅子座 |
| 張八 | 處女座 |
| 王九 | 天秤座 |
| 張言 | 天蠍座 |
| 李志 | 射手座 |
| 王月 | 水瓶座 |
| 張欣 | 雙魚座 |
+----------+----------+
11 rows in set
2.外連接:
(1)左連接,返回左表的所有數據以及右表對應的數據。 Left join
mysql> select starname,username from users as u left join star s on u.starid=s.starid;
+----------+----------+
| starname | username |
+----------+----------+
| 白羊座 | 張一 |
| 金牛座 | 李二 |
| 雙子座 | 王三 |
| 巨蟹座 | 張四 |
| 獅子座 | 李五 |
| 處女座 | 張八 |
| 天秤座 | 王九 |
| 天蠍座 | 張言 |
| 射手座 | 李志 |
| 水瓶座 | 王月 |
| 雙魚座 | 張欣 |
+----------+----------+
11 rows in set
(2)右連接,返回右表的所有數據以及左表相對應的數據。Right join
(五)子查詢
在某些特定的業務需求下,當進行查詢的時候,需要的條件是另外一個 select 語句的結果,這個時候,就要用到子查詢。
用於子查詢的關鍵字主要包括 in、not in、=、!=、exists、not exists ……
1. 比較運算符 > < = !=
mysql> select username,height from users where height >(select height from users where username="張四");
+----------+--------+
| username | height |
+----------+--------+
| 李二 | 155 |
| 王三 | 160 |
| 李五 | 160 |
| 張八 | 168 |
| 王九 | 170 |
| 張言 | 176 |
| 李志 | 180 |
| 王月 | 176 |
| 張欣 | 180 |
+----------+--------+
9 rows in set
2. 子查詢 in
mysql> select starid,username,height from users where starid in(select starid from users where height>170);
+--------+----------+--------+
| starid | username | height |
+--------+----------+--------+
| 8 | 張言 | 176 |
| 9 | 李志 | 180 |
| 11 | 王月 | 176 |
| 12 | 張欣 | 180 |
+--------+----------+--------+
4 rows in set
3. Exists
(1)mysql> select starname from star where exists(select * from star where starid=13);
Empty set
(2) mysql> select starname from star where exists(select * from star where starid<13);
+----------+
| starname |
+----------+
| 白羊座 |
| 金牛座 |
| 雙子座 |
| 巨蟹座 |
| 獅子座 |
| 處女座 |
| 天秤座 |
| 天蠍座 |
| 射手座 |
| 摩羯座 |
| 水瓶座 |
| 雙魚座 |
+----------+
12 rows in set
Exists()括號內為真 則前面的語句執行,為假則前面的語句不執行;EXISTS也可以作為WHERE 語句的子查詢,但一般都能用 IN子查詢替換。
4. not exists
與exists剛好是反的。
5. mysql> select sum(avg_score) from(select avg(score) as avg_score from score group by name) as t1;
+----------------+
| sum(avg_score) |
+----------------+
| 215.8333 |
+----------------+
1 row in set
mysql>