程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> MYSQL數據庫 >> MySQL綜合教程 >> mysql學習筆記之八(單表數據記錄查詢)

mysql學習筆記之八(單表數據記錄查詢)

編輯:MySQL綜合教程

mysql學習筆記之八(單表數據記錄查詢)



查詢數據記錄,就是指從數據庫對象中獲取所要求的數據記錄。mysql中提供了各種不同方式的數據查詢方法。


一、簡單數據記錄查詢
 

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

*查詢所有字段數據select * from t_name;
*查詢指定字段數據select field1,...,fieldn from t_name;如果指定field1,...,fieldn為所有的列就成了查詢所有字段了。
*避免重復數據查詢--distinctselect 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.3select 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 value2select field1,field2,...,fieldn from t_name where field not between value1 and value2
select product from t_product where price between 3.3 and 6select 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,...,fieldnfrom t_namewhere conditionorder 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,...,fieldnfrom t_namewhere conditionlimit offser_start,row_count功能:
*不指定初始位置方式不指定初始位置,默認為0,表示從第一條記錄開始顯示.limit row_countselect 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還是非nullcount(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_namewhere conditiongroup by field;注意:在具體分組查詢時,分組所依據的字段上的值一定要具有重復值,否則將沒有任何實際意義。


 

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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved