MySQL的深入淺出(二)—— 查詢

select查詢


mysql中單引號代表具體的值,反引號代表字段或者表名稱

    select 'c_id' as 序號;

輸出:

+------+
| 序號 |
+------+
| c_id |
+------+
select '輸出值' as 列名稱;

select c_id as 序號;
select `c_id` as 序號,c_id as '序號' from commodity;
正常輸出表中所有序號
總結:	
   as前的名稱:
    	沒有單引號或者用反引號括起來,輸出相應的字段值               
    	有單引號,全部輸出單引號中的值
		
   as後的名稱: 僅僅作爲輸出列名,有沒有單引號,反引號影響不大

查詢方式一

select * from table_name;

查詢表中所有數據

查詢方式二

(字段之間有逗號,除了最後一個):

select field1,field2, ... ,fieldn from table_name;
查詢指定字段的數據,field1,field2....的順序無所謂,僅僅決定了輸出順序

查詢方式三

(字段之間有逗號,除了最後一個):

->select field1 as name_one field2 as name_two 
->from table_name;

例子:

select c_id as 序號,c_name as 名稱 from commodity;

查詢方式四(四則運算)

例子:

->select c_id as 序號,c_name as 名稱,
->c_inprice as 進價,
->c_outprice as 售價,
->c_inprice-c_outprice as 利潤
->from commodity;
    注意:null值和其他值運算得到的數值是null

查詢方式五(distinct)

SELECT DISTINCT field1,field2, ... FROM table_name;
#使用DISTINCT關鍵字去掉重複數據
例:select distinct c_madein from commodity;

查詢方式六

(關係運算符和邏輯運算符,between)

select c_id,c_name 
from commodity where (c_id>10 and c_id<50) and c_type=3;
//查詢id大於10,id小於50且類型爲書籍的商品多個and條件建議使用括號分開
			
select c_id,c_name from commodity where (c_id between 10 and 50) and c_type=3;
#注意:between 關鍵字包括兩個端點
			
select c_id,c_name from commodity where (c_id not between 10 and 50) and c_type=3;
#注意:not between不包括兩個端點
關係運算符:>,<,>=,<=,!=
邏輯運算符:and,or,not,

查詢方式七(is null和is not null非空判斷)

select c_id,c_name,c_outprice  from commodity where c_outprice is null;

select c_id,c_name,c_outprice  from commodity where c_outprice is not null;

查詢方式八(in和not in)

select c_name,c_inprice from commodity where c_inprice in (10,23,778,45,34);
#in:裏面的數字之間是或關係
#進價在in範圍中的數據
	
select c_name,c_inprice from commodity where c_inprice not in (10,23,778,45,34);
#not in:裏面的數字之間是且關係

查詢方式九(like)

LIKE要和通配符一起用,不然沒有用通配符的LIKE效果等同於“=”,常用的通配符有“_”和“%”;

“_”:匹配單個字符
“%”:匹配任意個字符

select c_name from commodity where c_name like '%玩具%';
#商品名稱中帶有玩具二字的名稱
注意:	
    1)like後的匹配字段屬於值,用單引號括起來
    2)like關鍵字會讓數據庫主動放棄使用索引,降低效率;儘量不要使用like關鍵字

查詢方式十(order by)

SELECT field1,field2,... FROM table_name [WHERE CONDITION] ORDER BY fieldn [ASC|DESC];
1),null數據認爲是最小值
2),去除null,where相應字段中使用is not null
3)默認升序asc;默認對主鍵進行排序
4)字符串排序:	英文:按照字母順序排序

查詢方式十一(limit)

select * from table limit 2; 只輸出前2;
select * from table limit 0,5;從第0行開始,輸出5;

select c_name,c_inprice from commodity where c_type=1 order by c_inprice desc limit 5;
  • limit關鍵字優化

翻頁效果:

select * from table limit (page-1)*pageNumber,pageNumber;
#page當前頁序號,pageNumber每一頁數據量
#limit每次都從第一條開始查詢,效率太低

解決方案:如同字典一樣,安裝目錄(index,索引)

select * from table where id>=(page-1)*pageNumber and id<(page-1)*pageNumber+pageNumber;

聚合函數

很多情況下,都需要一些統計彙總工作,比如整個公司的人數, 某部門的人數等等,這時候就用到統計函數(聚合函數)了,它們分別爲:

COUNT()函數:統計記錄數;
AVG()函數:求平均值;
SUM()函數:求和;
MAX()函數:求最大數; 
MIN()函數:求最小數

其中,COUNT()函數可以通過以下兩種方法來實現統計:

count(*)使用方式,實現對錶中記錄進行統計,不管是否包含 NULL還是NOT NULL count(field)使用方法對指定字段進行統計,將忽略NULL值!

還有,如果表中無數據,count()函數返回的是0,其它函數返回 null;
mysql> #登錄驗證
mysql> #cu_name cu_phone
mysql> select * from customer where cu_name='' and cu_phone='';
Empty set (0.00 sec)

mysql> #java拿到的是什麼? null -> 空指針異常 所以可以用couut(*)來避免空指針
mysql> select count(*) from customer where cu_name='' and cu_phone='';
+----------+
| count(*) |
+----------+
|        0 |
+----------+
1 row in set (0.00 sec)

mysql> #if(count>0){//TUDO:登錄成功}else{//TUDO:登錄失敗}

分組查詢(group by)

group by 一般和聚合函數一起出現是兄弟倆

-> select sum(c_inprice),avg(c_inprice),max(c_outprice)
-> from commodity
-> group by c_type;

+----------------+----------------+-----------------+
| sum(c_inprice) | avg(c_inprice) | max(c_outprice) |
+----------------+----------------+-----------------+
|           2448 |       116.5714 |            3000 |
|            600 |        33.3333 |             460 |
|           1053 |        52.6500 |             400 |
+----------------+----------------+-----------------+
3 rows in set (0.02 sec)


having

對分組查詢結果進行條件限制查詢

mysql> select c_type
    -> from commodity
    -> group by c_type
    -> having avg(c_inprice)>100;
+--------+
| c_type |
+--------+
|      1 |
+--------+
1 row in set (0.00 sec)

mysql> #這個having中不需要出現在結果表中 是從5.7版本纔開始支持的
where和having的區別
1.類型:
    “Where”是一個約束聲明,在查詢數據庫的結果返回之前對數據庫中的查詢條件進行約束,即在結果返回之前起作用,且where後面不能使用“聚合函數”;
    
    “Having”是一個過濾聲明,所謂過濾是在查詢數據庫的結果返回之後進行過濾,即在結果返回之後起作用,並且having後面可以使用“聚合函數”。

2.使用的角度:
where後面之所以不能使用聚合函數是因爲where的執行順序在聚合函數之前,
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章