mysql學習筆記之八(單表資料記錄查詢),mysql學習筆記

來源:互聯網
上載者:User

mysql學習筆記之八(單表資料記錄查詢),mysql學習筆記

查詢資料記錄,就是指從資料庫物件中擷取所要求的資料記錄。mysql中提供了各種不同方式的資料查詢方法。


一、簡單資料記錄查詢


select field1,field2,...,fieldn from t_name

*查詢所有欄位資料
select * from t_name;

*查詢指定欄位資料
select field1,...,fieldn from t_name;
如果指定field1,...,fieldn為所有的列就成了查詢所有欄位了。

*避免重複資料查詢--distinct
select distinct field1,...,fieldn from t_name

*實現數學四則運算資料查詢
+ - * / %
select price,price*10 from t_product


*設定顯示格式資料查詢
select concat('每公斤',product,'的價格',price*2) per from t_product

二、條件資料記錄查詢


select field1,field2,...,fieldn from t_name where condition
功能:
*帶有關係運算子和邏輯運算子的條件資料查詢
> < = <= >= !=(<>) 
&&(AND) ||(OR) !(NOT) XOR(邏輯異或)


select product from t_product where price<4.0 && price>3.3
select price from t_product where product='pear';
select price from t_product where PRODUCT='pear';
兩句的查詢結果一樣,也就是說,在這裡大小寫忽略了。
alter table t_product add PRODUCT float(8,2);
ERROR:Duplicate column name 'PRODUCT'

*帶有between and 關鍵字的條件資料查詢
select field1,field2,...,fieldn from t_name where field between value1 and value2
select field1,field2,...,fieldn from t_name where field not between value1 and value2

select product from t_product where price between 3.3 and 6
select product from t_product where price not between 3.3 and 6

*帶is null關鍵字的條件資料查詢
select field1,...,fieldn from t_name where field is null;
select field1,...,fieldn from t_name where field is not null;

*帶in關鍵字的條件資料查詢
判斷欄位的數值是否在指定集合中的條件查詢。
select field1,...,fieldn from t_name where field in (value1,...,valuen);
select product from t_product where price in (43.40,6.5,8.9);
select product from t_product where price not in (43.40,6.5,8.9);


注意:
在具體使用關鍵字in時,查詢的集合中如果存在null,則不會影響查詢;如果使用關鍵字not in,查詢的集合中如果存在null,則不會有任何的查詢結果


*帶like關鍵字的模糊資料查詢
select field1,...,fieldn from t_name where field like value;
select filed1,...,fieldn from t_name where field not like value;
select field1,...,fieldn from t_name where not field like value;
like關鍵字支援的萬用字元:
-:該萬用字元值能匹配單個字元
%:該萬用字元值可以匹配任意長度的字串。
mysql不僅對於關鍵字不區分大小寫,對於欄位資料記錄也不區分大小寫。
select * from t_product where product like 'a%'
select * from t_product where product like 'A%'
兩句的查詢結果一樣
select * from t_product where product like '_d%';
select * from t_product where !(product like '_d%');


三、排序資料記錄查詢
select field1,field2,...,fieldn
from t_name
where condition 
order by fieldm1 [asc|desc][,fieldm2 [asc|desc],]
功能:
*按照單欄位排序
關鍵字order by後面只接一個欄位,查詢結果在顯示時將按照該欄位進行排序。
在mysql中,null值為最小值,因此升序時將最先顯示
1、升序排序
select * from t_product order by price [asc]
預設為升序
2、降序排序
select * from t_product order by price desc;
*按照多欄位排序
select * from t_product order by price asc,id desc;
四、限制資料記錄查詢數量
select field1,...,fieldn 
from t_name
where condition
limit offser_start,row_count
功能:
*不指定初始位置方式
不指定初始位置,預設為0,表示從第一條記錄開始顯示.
limit row_count
select product from t_product limit 5;
注意:如果row_count大於查詢的記錄數,則顯示所有的查詢記錄數;如果row_count小於查詢記錄數,則顯示row_count條記錄。

*指定初始位置方式
limit關鍵字經常被應用在分頁系統中,對於第一頁資料記錄,可以通過不指定初始位置來實現,但是對於第二頁等其他頁面則必須指定初始位置(offset_start),否則將無法實現分頁功能。除此之外,limit關鍵字還經常跟order by一起使用,即先對查詢結果進行排序,然後顯示其中部分資料記錄。

select product from t_product limit 4,5;
從第五條記錄開始,顯示五條記錄。


五、統計函數和分組資料記錄查詢
count(),avg(),sum(),max(),min()
在具體應用中,統計函數經常跟分組一起使用。
注意:雖然資料值沒有重複也可以進行分組,但是不建議使用,因為一條資料記錄也可以分成一組,但是沒有任何實際意義
select function(field) 
from t_name
[where condition]
1、統計資料記錄條數
count(*):實現對錶中資料記錄進行統計,不管表欄位中包含的是null還是非null
count(field):實現對指定欄位的記錄進行統計,在具體統計時將忽略null值

2、統計計算平均值
avg(field):實現對指定欄位的平均值進行計算,在具體統計時將忽略null值。
3、統計計算求和
sum(field):實現計算指定欄位值之和,在統計時忽略null值

4、統計最大值和最小值
max(field)和min(field)
       用來實現統計資料計算求最大值和最小值,這些函數可以用來計算指定欄位中的最大值和最下值或符合特定條件的指定欄位值中對的最大值和最小值。
注意:
關於mysql所支援的統計數,如果所操作的表中沒有任何資料記錄,則count()函數會返回資料0,而其他函數則會返回null
六、分組資料函數
1、簡單分組查詢
在具體使用統計函數時,都是針對錶中所有記錄數或指定特定條件(where子句)的資料記錄進行統計計算。在現實應用中,經常會把所有的資料記錄進行分組,然後才對這些分組後的資料記錄進行統計計算。
select function()
from t_name
where condition
group by field;
注意:在具體分組查詢時,分組所依據的欄位上的值一定要具有重複值,否則將沒有任何實際意義。



2、實現統計功能分組查詢
        mysql如果只實現簡單的分組查詢,是沒有任何實際意義的,因為關鍵字group by單獨使用時,預設查詢出每個分組中隨機一條記錄,具有很大的不確定性。
        group_concat():實現顯示每一個分組中的指定欄位值。
select group_concat(field)
from t_name
where condition
group by field
例:
select price,count(price) as count,group_concat(product)
from t_product
group by price;
3、實現多個欄位分組查詢
select group_concat(field),function(field)
from t_name
where condition
group by field1,...,fieldn;
上述語句中首先會按照欄位field1進行分組,然後針對每組按照field2進行分組,一次類推。
4、實現having子句限定分組查詢
如果想實現對分組進行條件限制,決不能通過關鍵字where來實現。因為該關鍵字主要用來實現條件限制資料記錄。因此也就有了having來實現條件限制分組資料記錄。
select function(field)
from t_name
where condition
group by field1,...,fieldn
having condition;

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.