例題SQL語句詳解-資料庫基本操作10-查詢語句
阿新 • • 發佈:2018-12-10
|版權宣告:本文為博主原創文章,未經博主允許不得轉載。轉載請附上原連結,部落格地址:https://blog.csdn.net/sgsgy5
1.6 查詢語句
語法:select [選項] 列名 [from 表名] [where 條件] [group by 分組] [order by 排序][having 條件] [limit 限制]
1.6.1 欄位表示式
mysql> select '鋤禾日當午';
+------------+
| 鋤禾日當午 |
+------------+
| 鋤禾日當午 |
+------------+
mysql> select 10*10;
+-------+
| 10*10 |
+-------+
| 100 |
+-------+
通過as給欄位取別名
mysql> select '鋤禾日當午' as content;
+------------+
| content |
+------------+
| 鋤禾日當午 |
+------------+
1 row in set (0.00 sec)
mysql> select 10*10 as result;
+--------+
| result |
+--------+
| 100 |
+--------+
1 row in set (0.00 sec)
多學一招:as可以省略
mysql> select 10*10 result;
+--------+
| result |
+--------+
| 100 |
+--------+
1 row in set (0.00 sec)
1.6.2 from子句
from:來自,from後面跟的是資料來源。資料來源可以有多個。返回笛卡爾積。
插入測試表
mysql> create table t1(
-> id int,
-> name varchar(10)
-> );
Query OK, 0 rows affected (0.05 sec)
mysql> create table t2(
-> field1 varchar(10),
-> field2 varchar(10)
-> );
Query OK, 0 rows affected (0.00 sec)
mysql> insert into t1 values (1,'tom'),(2,'berry');
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> insert into t2 values ('333','333'),('444','444');
Query OK, 2 rows affected (0.02 sec)
Records: 2 Duplicates: 0 Warnings: 0
測試多個數據源
mysql> select * from t1,t2; # 返回笛卡爾積
+------+-------+--------+--------+
| id | name | field1 | field2 |
+------+-------+--------+--------+
| 1 | tom | 333 | 333 |
| 2 | berry | 333 | 333 |
| 1 | tom | 444 | 444 |
| 2 | berry | 444 | 444 |
+------+-------+--------+--------+
4 rows in set (0.00 sec)
1.6.3 dual表
dual表是一個偽表。在有些特定情況下,沒有具體的表的參與,但是為了保證select語句的完整又必須要一個表名,這時候就使用偽表。
mysql> select 10*10 as result from dual; #dual表是用來保證select語句的完整性。
+--------+
| result |
+--------+
| 100 |
+--------+
1.6.4 where子句
where後面跟的是條件,在資料來源中進行篩選。返回條件為真記錄
MySQL支援的運算子
>
大於<
小於>=
<=
=
!=
- and 與
- or 或
- not 非
mysql> select * from stu where stusex='男'; # 查詢性別是男的記錄
mysql> select * from stu where stuage>=20; # 查詢年齡不低於20的記錄
思考:如下程式碼輸出什麼
select * from stu where 1 # 返回所有資料庫
select * from stu where 0 #返回空記錄
思考:如何查詢北京和上海的學生
mysql> select * from stu where stuaddress='上海' or stuaddress='北京';
+--------+---------+--------+--------+---------+------------+------+------+
| stuNo | stuName | stuSex | stuAge | stuSeat | stuAddress | ch | math |
+--------+---------+--------+--------+---------+------------+------+------+
| s25301 | 張秋麗 | 男 | 18 | 1 | 北京 | 80 | NULL |
| s25302 | 李文才 | 男 | 31 | 3 | 上海 | 77 | 76 |
| s25303 | 李斯文 | 女 | 22 | 2 | 北京 | 55 | 82 |
| s25320 | Tom | 男 | 24 | 8 | 北京 | 65 | 67 |
+--------+---------+--------+--------+---------+------------+------+------+
1.6.5 in | not in
上面的查詢上海和北京的學生的SQL可以通過in語句來實現
mysql> select * from stu where stuaddress in ('北京','上海');
練習:
1、查詢學號是s25301,s25302,s25303的學生
mysql> select * from stu where stuno in ('s25301','s25302','s25303');
2、查詢年齡是18,19,20的學生
mysql> select * from stu where stuage in(18,19,20);
3、查詢不是北京和上海的學生
mysql> select * from stu where stuaddress not in ('北京','上海');
1.6.6 between…and|not between…and
查詢某個範圍的記錄
1、查詢年齡在18~20之間的學生
mysql> select * from stu where stuage>=18 and stuage<=20; # 方法一
mysql> select * from stu where stuage between 18 and 20; # 方法二
2、查詢年齡不在18~20之間的學生
mysql> select * from stu where stuage<18 or stuage>20; #方法一
mysql> select * from stu where not (stuage>=18 and stuage<=20);
mysql> select * from stu where stuage not between 18 and 20;
1.6.7 is null | is not null
腳下留心:查詢一個為空的欄位不能用等於,必須用is null
查詢缺考的學生
mysql> select * from stu where ch is null or math is null; # 查詢缺考的人
+--------+----------+--------+--------+---------+------------+------+------+
| stuNo | stuName | stuSex | stuAge | stuSeat | stuAddress | ch | math |
+--------+----------+--------+--------+---------+------------+------+------+
| s25301 | 張秋麗 | 男 | 18 | 1 | 北京 | 80 | NULL |
| s25304 | 歐陽俊雄 | 男 | 28 | 4 | 天津 | NULL | 74 |
+--------+----------+--------+--------+---------+------------+------+------+
查詢參加考試的學生
mysql> select * from stu where ch is not null and math is not null;
1.6.8 聚合函式
- sum() 求和
- avg() 求平均值
- max() 求最大值
- min() 求最小值
- count() 求記錄數
#求語文總分、語文平均分、語文最高分、語文最低分、總人數
mysql> select sum(ch) '語文總分',avg(ch) '語文平均分', max(ch) '語文最高分',min(ch) '語文最低分',count(*) '總人數' from stu;
+----------+------------+------------+------------+--------+
| 語文總分 | 語文平均分 | 語文最高分 | 語文最低分 | 總人數 |
+----------+------------+------------+------------+--------+
| 597 | 74.6250 | 88 | 55 | 9 |
+----------+------------+------------+------------+--------+
1 row in set (0.00 sec)
1.6.9 萬用字元
- _ [下劃線] 表示任意一個字元
- % 表示任意字元
練習
1、滿足“T_m”的有(A、C)
A:Tom B:Toom C:Tam D:Tm E:Tmo
2、滿足“T_m_”的有(B、C )
A:Tmom B:Tmmm C:T1m2 D:Tmm E:Tm
3、滿足“張%”的是(A、B、C、D)
A:張三 B:張三丰 C:張牙舞爪 D:張 E:小張
4、滿足“%諾基亞%”的是(A、B、C、D)
A:諾基亞2100 B:2100諾基亞 C:把我的諾基亞拿過來 D:諾基亞
1.6.10 模糊查詢(like)
# 查詢姓張的同學
mysql> select * from stu where stuname like '張%';
+--------+---------+--------+--------+---------+------------+------+------+
| stuNo | stuName | stuSex | stuAge | stuSeat | stuAddress | ch | math |
+--------+---------+--------+--------+---------+------------+------+------+
| s25301 | 張秋麗 | 男 | 18 | 1 | 北京 | 80 | NULL |
+--------+---------+--------+--------+---------+------------+------+------+
1 row in set (0.00 sec)
#例題
mysql> select * from stu where stuname like 'T_m';
+--------+---------+--------+--------+---------+------------+------+------+
| stuNo | stuName | stuSex | stuAge | stuSeat | stuAddress | ch | math |
+--------+---------+--------+--------+---------+------------+------+------+
| s25320 | Tom | 男 | 24 | 8 | 北京 | 65 | 67 |
+--------+---------+--------+--------+---------+------------+------+------+
1 row in set (0.00 sec)