這篇博客主要介紹單鏈表的基本查詢的條件:
1、檢索數據表中的字段的數據:
select * from 表名;//數據量大的時候不易使用
select 字段1,字段2.... from 表名;//檢索指定字段的數據
2、比較大小:使用=、>、<、>=、<=、!=、<>檢索表中指定行的數據:
select 字段名1、2... from 表名
where 字段名 比較運算符 常量值;//常量值如果等於字符型,如漢字,要加引號
3、確定集合:使用in關鍵字檢索表中指定的數據內容:
select 字段名1、2.. from 表名
where 字段名 in(n1,n2,n3..);//in的使用情形就像點名,在In範偉內,點到誰誰出來
//否定:not in
4、確定範圍:使用between…and關鍵字:
select 字段名1、2... from 表名
where 字段名[not]between 上限制 and 下限制 ;//一個範圍是開始值,另一個範圍是結束值
5、字符匹配:使用like模糊匹配數據:
select 字段名1、2.. from 表名
where 字段名 like '字符%(_)';//%可以匹配任意長度字符,_只能匹配一個字符
eg.查詢學號帶0505:where stu_id like '%0505%';
6、查詢空值:使用 is null:
select 字段名1、2.. from 表名
where 字段名 is null;//null不代表0,也不代表空值,而是代表未知的狀態;
is not null //不是空值
7、去除重複結果:使用關鍵字:distinct:
select distinct 字段名1、2.. from 表名
[where 條件表達式]
8、多條件查詢:使用關鍵字:and、or:
select 字段名1、2.. from 表名
where 表達式1 and(or) 表達式2;//and是且,or是或,但是and和or一起使用的時候,and的優先級高於or
9、對查詢表排序:使用 order by :
select 字段名1、2.. from 表名
order by 字段名1、2... [asc[desc];//asc可以省略,默認升序,desc降序;排序的字段名可以多個
10、對查詢結果分組:
計數函數:
count(*) //統計元組的個數
count(([distinct]<字段名>))//統計一列中值的個數
求和函數:
sum(([distinct]<字段名>))//計算一列中值的總和,此列必須爲數值
求平均數值的函數:
AVG(([distinct]<字段名>))//計算一列值的平均值,此列必須爲數值
求最大(小)值函數:
MAX(MIN)(([distinct]<字段名>))//求一列值的最大(小)值,此列爲數值、字符、日期
注意:使用group by 字段名將查詢的是按結果的某一列
11、對查詢結果進行分組並篩選分組:
select 字段名1、2.. from 表名
[where 表達式1 and 表達式2]
group by 字段名 having 表達式;
注意:having 不能單獨使用,必須跟在group by短語之後,having短語的功能是對分組進行篩選,如:查詢某門課程數小於2的情況:group by cs_depart having count(cs_name)<2;
查詢的語法格式:
select [distinct]<目標列表達式>,......
from <表名>
[where <表達式>]//[]表示可以沒有該語句
[group by <列名>[having <表達式>]]
[order by <列名>[asc|desc];