MySQL 學習總結2

來源:互聯網
上載者:User

標籤:邏輯運算   參數   outer   外串連   left join   時間   log   邏輯運算子   單位   

 function 函數
函數的作用比較大,一般多用在select查詢語句和where條件陳述式之後。按照函數返回的結果,
可以分為:多行函數和單行函數;所謂的單行函數就是將每條資料進行獨立的計算,然後每條資料得到一條結果。
如:字串函數;而多行函數,就是多條記錄同時計算,得到最終只有一條結果記錄。如:sum、avg等
多行函數也稱為聚集合函式、分組函數,主要用於完成一些統計功能。MySQL的單行函數有如下特徵:
    單行函數的參數可以是變數、常量或資料列。單行函數可以接受多個參數,但返回一個值。
    單行函數就是它會對每一行單獨起作用,每一行(可能包含多個參數)返回一個結果。
    單行函數可以改變參數的資料類型。單行函數支援嵌套使用:內層函數的傳回值是外層函數的參數。
 
單行函數可以分為:
    類型轉換函式;
    位函數;
    流程式控制制語句;
    加密解密函數;
    資訊函數

單行函數

 
1、    char_length字元長度
select char_length(tel) from user;
 
2、    sin函數
select sin(age) from user;
select sin(1.57);
 
3、    添加日期函數
select date_add(‘2010-06-21‘, interval 2 month);
interval是一個關鍵字,2 month是2個月的意思,2是數值,month是單位
select addDate(‘2011-05-28‘, 2);
在前面的日期上加上後面的天數
 
4、    擷取當前系統時間、日期
select curdate();
select curtime();
 
5、    加密函數
select md5(‘zhangsan‘);
 
6、    Null 處理函數
select ifnull(birthday, ‘is null birthday‘) from user;
如果birthday為null,就返回後面的字串
 
select nullif(age, 245) from user;
如果age等於245就返回null,不等就返回age
 
select isnull(birthday) from user;
判斷birthday是否為null
 
select if(isnull(birthday), ‘birthday is null‘, ‘birthday not is null‘) from user;
如果birthday為null或是0就返回birthday is null,否則就返回birthday not is null;類似於三目運算子
 
7、    case 流程函數
case函數是一個流程式控制制函數,可以接受多個參數,但最終只會返回一個結果。
select name, 
age, 
(case sex
    when 1 then ‘男‘
    when 0 then ‘女‘
    else ‘火星人‘
    end
) sex
from user;

 

組函數

組函數就是多行函數,組函數是完成一行或多行結果集的運算,最後返回一個結果,而不是每條記錄返回一個結果。

1、    avg平均值運算
select avg(age) from user;
select avg(distinct age) from user;
 
2、    count 記錄條數統計
select count(*), count(age), count(distinct age) from user;
 
3、    max 最大值
select max(age), max(distinct age) from user;
 
4、    min 最小值
select min(age), min(distinct age) from user;
 
5、    sum 求和、聚和
select sum(age), sum(distinct age) from user;
select sum(ifnull(age, 0)) from user;
 
6、    group by 分組
select count(*), sex from user group by sex;
select count(*) from user group by age;
select * from user group by sex, age;
 
7、    having進行條件過濾
不能在where子句中過濾組,where子句僅用於過濾行。過濾group by需要having
不能在where子句中用組函數,having中才能用組函數
select count(*) from user group by sex having sex <> 2;

 

? 多表查詢和子查詢

資料庫的查詢功能最為豐富,很多時候需要用到查詢完成一些事物,而且不是單純的對一個表進行操作。而是對多個表進行聯集查詢,
MySQL中多表串連查詢有兩種規範,較早的SQL92規範支援,如下幾種表串連查詢:
    等值串連
    非等值串連
    外串連
    廣義笛卡爾積
SQL99規則提供了可讀性更好的多表串連文法,並提供了更多類型的串連查詢,SQL99支援如下幾種多表串連查詢:
    交叉串連
    自然串連
    使用using子句的串連
    使用on子句串連
    全部串連或者左右外串連
 
SQL92的串連查詢
SQL92的串連查詢文法比較簡單,多將多個table放置在from關鍵字之後,多個table用“,”隔開;
串連的條件放在where條件之後,與查詢條件直接用and邏輯運算子進行串連。如果條件中使用的是相等,
則稱為等值串連,相反則稱為非等值,如果沒有任何條件則稱為廣義笛卡爾積。
廣義笛卡爾積:select s.*, c.* from student s, classes c;
等值:select s.*, c.* from student s, classes c where s.cid = c.id;
非等值:select s.*, c.* from student s, classes c where s.cid <> c.id;
select s.*, c.name classes from classes c, student s where c.id = s.classes_id and s.name is not null;
 
SQL99串連查詢
1、交叉串連cross join,類似於SQL92的笛卡爾積查詢,無需條件。如:
select s.*, c.name from student s cross join classes c;
 
2、自然串連 natural join查詢,無需條件,預設條件是將2個table中的相同欄位作為串連條件,如果沒有相同欄位,查詢的結果就是空。
select s.*, c.name from student s natural join classes c;
 
3、using子句串連查詢:using的子句可以是一列或多列,顯示的指定兩個表中同名列作為串連條件。
如果用natural join的串連查詢,會把所有的相同欄位作為串連查詢。而using可以指定相同列及個數。
select s.*, c.name from student s join classes c using(id);
 
4、    join … on串連查詢,查詢條件在on中完成,每個on語句只能指定一個條件。
select s.*, c.name from student s join classes c on s.classes_id = c.id;
 
5、    左右外串連:3種外串連,left [outer] join、right [outer] join,串連條件都是通過用on子句來指定,條件可以等值、非等值。
select s.*, c.name from student s left join classes c on s.classes_id = c.id;
select s.*, c.name from student s right join classes c on s.classes_id = c.id;
 
    子查詢
    子查詢就是指在查詢語句中嵌套另一個查詢,子查詢可以支援多層嵌套。子查詢可以出現在2個位置:
    from關鍵字之後,被當做一個表來進行查詢,這種用法被稱為行內視圖,因為該子查詢的實質就是一個臨時視圖
    出現在where條件之後作為過濾條件的值
 
子查詢注意點:
    子查詢用括弧括起來,特別情況下需要起一個臨時名稱
    子查詢當做暫存資料表時(在from之後的子查詢),可以為該子查詢起別名,尤其是要作為首碼來限定資料列名時
    子查詢用作過濾條件時,將子查詢放在比較子的右邊,提供可讀性
    子查詢作為過濾條件時,單行子查詢使用單行運算子,多行子查詢用多行運算子
 
將from後面的子查詢當做一個table來用:
select * from (select id, name from classes) s where s.id in (1, 2);
當做條件來用:
select * from student s where s.classes_id in (select id from classes);
select * from student s where s.classes_id = any (select id from classes);
select * from student s where s.classes_id > any (select id from classes);

MySQL 學習總結2

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.