文章目錄
- 一、查詢語句
- 1. 基本查詢
- 2. 條件查詢
- 2.1 ?較運算符&邏輯運算符
- 2.2 模糊查詢
- 2.3 范圍查詢
- 2.4 判斷空
- 3. 其他復雜查詢
- 3.1 排序
- 3.2 聚合函數
- 3.3 分組
- 3.4 分頁查詢
- 二、回顧
- 1. 使? Navicat ?具中的命令列
- 2.命令?基本操作步驟
提示:以下是本篇文章正文內容,下面案例可供參考
一、查詢語句
1. 基本查詢
- 需求1: 準備商品數據, 查詢所有數據, 查詢部分字段, 起字段別名, 去重
查詢所有數據: select * from 表名;
select * from goods;
- 查詢部分字段: select 字段名1, 字段名2 from goods;
select goodsName, price from goods;
- 起字段別名: select 字段名 as ‘別名’ from goods;
select goodsName as '商品名稱', price as '價格' from goods;
- 注意: 別名的引號可以省略
select goodsName as 商品名稱, price as 價格 from goods;
- 注意: as 關鍵字也可以省略[掌握]
select goodsName 商品名稱, price 價格 from goods;
- 起別名的作?: 1> 美化數據結果的顯示效果 2> 可以起到隱藏真正字段名的作?
- 另: 除了可以給字段起別名以外, 還可以給數據表起別名(連接查詢時使?)
- 去重: select distinct(字段名) from goods;
- 效果: 將?標字段內重復出現的數據只保留?份顯示
- ?需求: 顯示所有的公司名稱
select distinct(company) from goods;
2. 條件查詢
2.1 ?較運算符&邏輯運算符
-- 需求2: 查詢價格等于30并且出?并夕夕的所有商品信息
select * from goods;
-- 查詢價格等于30 : ?較運算符(特殊: (?于等于)>=/(?于等于)<=/(不等于)!=/<>)
select * from goods where price=30;-- 并且出?并夕夕的所有商品信息 : 邏輯運算符(and(與)/or(或)/not(?))
-- 注意: 作為查詢條件使?的字符串必須帶引號!
select * from goods where price=30 and company='并夕夕';-- 補充需求: 查詢價格等于30但不出?并夕夕的所有商品信息
select * from goods where not company='并夕夕' and price=30;
-- 注意: not 與 and 和 or (左右兩邊連接條件)不同之處在于, not 只對??右側的條件有作?(右邊連接條件)
select * from goods where price=30 and not company='并夕夕';
2.2 模糊查詢
-- 需求3: 查詢全部?次性?罩的商品信息
-- 模糊查詢: like 和符號 %(任意多個字符)/_(任意?個字符)
-- 注意: 作為查詢條件使?的字符串必須帶引號!
-- 注意: 如果需要控制字符數量, 需要使?_, 并且有?個字符就使??個_
-- %關鍵詞% : 關鍵詞在中間
select * from goods where remark like '%?次性%';
-- %關鍵詞 : 關鍵詞在末尾
select * from goods where remark like '%?次性';
-- 關鍵詞% : 關鍵詞在開頭
select * from goods where remark like '?次性%';
2.3 范圍查詢
-- 需求4: 查詢所有價格在30-100的商品信息
-- 范圍查詢: 1> ?連續范圍: in 2> 連續范圍: between ... and ...
select * from goods where price between 30 and 100;
-- 注意: between ... and ... 的范圍必須是從?到?
select * from goods where price between 100 and 30;
2.4 判斷空
-- 需求5: 查詢沒有描述信息的商品信息
-- 注意: 在 MySQL 中, 只有顯示為 NULL 的才為空! 其余空?可能是空格/制
表符(tab)/換?符(回?鍵)等空?符號
-- 判斷空: 1> 為空: is null 2> 不為空(雙重否定表肯定): is not null
select * from goods where remark is null;
-- 補充需求: 查詢有描述信息的所有商品
select * from goods where remark is not null;
3. 其他復雜查詢
3.1 排序
-- 需求6: 查詢所有商品信息, 按照價格從?到?排序, 價格相同時, 按照數量少到多排序
-- select * from 表名 order by 列1 asc|desc,列2 asc|desc,...
-- 說明: order by 排序, asc : 升序, desc : 降序
-- 注意: 排序過程中, ?持連續設置多條排序規則, 但離 order by 關鍵字越近, 排序數據的范圍越?!
select * from goods order by price desc;
select * from goods order by price desc, count asc;-- 注意: 默認排序為升序, asc 可以省略
select * from goods order by price desc, count;
3.2 聚合函數
-- 需求7: 查詢以下信息: 商品信息總條數; 最?商品價格; 最低商品價格; 商品平均價格; ?次性?罩的總數量
-- 聚合函數: 系統提供的?些可以直接?來獲取統計數據的函數-- 商品信息總條數: count(字段): 查詢總記錄數
select count(*) from goods;
-- 注意: 統計數據總數, 建議使?* , 如果使?某?特定字段, 可能會造成數據總數錯誤!-- 最?商品價格: max(字段): 查詢最?值
select max(price) from goods;-- 最低商品價格: min(字段): 查詢最?值
select min(price) from goods;-- 商品平均價格: avg(字段): 求平均值
select avg(price) from goods;-- ?次性?罩的總數量: sum(): 求和
-- 注意: 此處的 count 是數據表中字段名!
select sum(count) from goods where remark like '%?次性%';-- 擴展: 在需求允許的情況下, 可以?次性在?條 SQL語句中, 使?所有的聚合函數
select count(*), max(price), min(price), avg(price) from goods;
3.3 分組
-- 需求8: 查詢每家公司的商品信息數量
-- 分組: select 字段1,字段2,聚合... from 表名 group by 字段1,字段2...
-- 說明: group by : 分組-- 注意:
-- 1> ?般情況, 使?哪個字段進?分組, 那么只有該字段可以在 * 的位置處使?, 其他字段沒有實際意義(只要?組數據中的?條)
-- 2> 分組操作多和聚合函數配合使?
select count(*) from goods group by company;
select * from goods;
select company, count(*) from goods group by company;
-- 說明: 其他字段沒有實際意義(只要?組數據中的?條)
select price, count(*) from goods group by company;-- 擴充: 分組后條件過濾
-- 說明: group by 后增加過濾條件時, 需要使? having 關鍵字
-- 注意:
-- 1. group by 和 having ?般情況下需要配合使?
-- 2. group by 后邊不推薦使? where 進?條件過濾
-- 3. having 關鍵字后側可以使?的內容與 where 完全?致(?較運算符/邏輯運算符/模糊查詢/判斷空)
-- 3. having 關鍵字后側允許使?聚合函數-- where 和 having 的區別:
-- where 是對 from 后?指定的表進?數據篩選,屬于對原始數據的篩選
-- having 是對 group by 的結果進?篩選
-- having 后?的條件中可以?聚合函數,where 后?不可以
3.4 分頁查詢
-- 需求9: 查詢當前表當中第5-10?的所有數據
-- 分?查詢: select * from 表名 limit start,count
-- 說明: limit 分?; start : 起始?號; count : 數據?數
-- 注意: 計算機的計數從 0 開始, 因此 start 默認的第?條數據應該為 0, 后續數據依次減1
-- 過渡需求: 獲取前 5 條數據
select * from goods limit 0, 5;
-- 注意: 如果默認從第?條數據開始獲取, 則 0 可以省略!
select * from goods limit 5;
-- 需求:
select * from goods limit 4, 6;-- 擴展 1: 根據公式計算顯示某?的數據
-- 已知:每?顯示m條數據,求:顯示第n?的數據
-- select * from 表名 limit (n-1)*m, m
-- 示例: 每?顯示 4 條數據, 求展示第 2 ?的數據內容
select * from goods limit 0, 4; -- 第1?(有數據)
select * from goods limit 4, 4; -- 第2?(有數據)
select * from goods limit 8, 4; -- 第3?(有數據)
select * from goods limit 12, 4; -- 第4?(?共 12 條數據, 每?顯示 4 條, 沒有第 4 ?數據)-- 擴展 2: 分?的其他應?
-- 需求: 要求查詢商品價格最貴的數據信息
select * from goods order by price desc limit 1;
-- 進階需求: 要求查詢商品價格最貴的前三條數據信息
select * from goods order by price desc limit 3;