mysql4

-- 函數(shù):就是一個方法(模塊),它是可重用的

-- 1.系統(tǒng)函數(shù)? 2.自定義函數(shù)

-- IFNULL(expr1,expr2) 說明;expr1/expr2 輸入?yún)?shù)

-- 文本函數(shù)

-- CHARACTER_LENGTH(str)(str:參數(shù)(用戶輸入的數(shù)據(jù))):返回文本str的字符個數(shù)(長度)(字母個數(shù))

select CHARACTER_LENGTH('dafgasgagasggasg') from dual (dual 系統(tǒng)自帶測試表格)

select ename,CHARACTER_LENGTH(ename) from emp

-- CONCAT(str1,str2,...)作用:將參數(shù)str1,str2,...strn合并成一個字符串

select CONCAT('aaa','bbb','ccc') from? dual

查詢ename和job,顯示的格式是"TOM的職位是PROGRAMER"

SELECT ename,job,CONCAT(ename,'的職位是',job) from emp

SELECT ename,job,CONCAT(列名,'中間字',列名) from emp

-- LOWER(str) 轉(zhuǎn)小寫

-- UPPER(str) 轉(zhuǎn)大寫

select lower('TOM'),UPPER('good') from? dual

select lower(ename ) from emp

-- SUBSTR(str,start,len):表示截取子串

-- SUBSTR(原始字符串,起始字符,從起始字符后多少個字符)

--? 要將文本 str 從 start起始位置 截取 len個長度的字符

SELECT SUBSTR('safasfasdsad',2,3)from DUAL 從第2個開始截取 截取3個字符

select ename,SUBSTR(ename,2,3) from emp

-- TRIM(str): 截取字符串的首尾空格

-- 函數(shù)是可以嵌套的 函數(shù)1(函數(shù)2(函數(shù)3)) 注意:先會執(zhí)行最內(nèi)層函的數(shù)

select trim('? test? ? ') from dual

select CHAR_LENGTH('? test? trim? '),CHARACTER_LENGTH(TRIM(' test? trim? '))from dual

-- 數(shù)值函數(shù)

-- round(x)

-- round(x,d)(數(shù)值,保留幾位小數(shù))

select round(2.5) from DUAL

select round(15.3573,2) from dual

-- TRUNC 表示直接去除小數(shù)部分

-- MOD(N,M):取余(N除以M之后的余數(shù))

SELECT mod(5,3) from dual

-- 時間函數(shù)

-- 當前的時間

-- now()獲取當前的日期+時間 年-月-日 小時:分:秒

-- CURDATE()獲取當前的日期 年-月-日

-- CURTIME()獲取當前的時間 小時:分:秒

select NOW(),CURDATE(),CURRENT_TIME() from dual

-- DATE_ADD(date,INTERVAL n(數(shù)字) type(類型)):追加時間

-- type: YEAR MONTH DAY HOUR MINUTE SECOND

select NOW(),DATE_ADD(NOW(),INTERVAL 1 hour) from DUAL

-- 計算所在月份的的最后一天 LAST_DAY(date類型 填列名)

-- 計算員工入職日期當月的最后一天

select ename,hiredate, LAST_DAY(hiredate) from emp

-- 分組函數(shù):多行數(shù)據(jù)作為參數(shù)最后得到統(tǒng)計后的結(jié)果,一般用于數(shù)據(jù)統(tǒng)計

-- 常用的分組函數(shù);AVG() MAX() MIN() SUM() COUNT()

SELECT avg(sal),max(sal),min(sal),sum(sal),count(*) from emp

1. -- 求平均值

-- 計算所有員工月收入的平均值

select avg(sal+IFNULL(comm,0)) from emp

-- 因為平均值不屬于某個人,所以結(jié)果是不正確的,在其他的數(shù)據(jù)庫中這種寫法會報錯

-- 因為分組函數(shù)只會返回一行一列的數(shù)據(jù),而ename可以返回多個值,所以不能混用

select ename,avg(sal+IFNULL(comm,0)) from emp

2.-- 求總和SUM()

select SUM(sal) from emp

3.-- 計算值(統(tǒng)計總數(shù))COUNT():求總和的記錄數(shù)(行數(shù))

select COUNT(*) from? emp (全部 所以用*)

select COUNT(comm(列名,有空值不顯示)) from emp -- 不會計算空值 結(jié)果為4條數(shù)據(jù)

4.-- 求最大值 max

select max(sal) from emp

-- 查詢?nèi)肼毴掌谧钔淼?/p>

select max(hiredate) from emp

5.-- 求最小值 MIN()

-- 查詢?nèi)肼毴掌谧钤绲?/p>

select min(hiredate) from emp

SELECT AVG( sal),SUM(sal),MAX(sal), MIN(sal),COUNT(*) from emp

-- GROUP BY 分組語句

-- GROUP BY 語句用于結(jié)合分組函數(shù),根據(jù)一個或多個列對結(jié)果集進行分組(分組語句)。

-- 編寫SQL的語句的順序:select...from...where...group by...order by

-- 查詢每個部門的平均工資

-- 分組函數(shù)avg(sal)與多值的列deptno是不能混用的

-- select 后面有幾個多值的列 group by 后要將所以的多值的列寫上

-- 分組可以作用在多個列上。 例如 GROUP BY A,B 先按照A進行分組,在A 分組的內(nèi)部在按照B來分組

select * from emp

select deptno,AVG(sal)? from emp? GROUP BY deptno

-- select 后面有幾個多值的列 group by 后要將所以的多值的列寫上

-- 分組可以作用在多個列上。 例如 GROUP BY A,B 先按照A進行分組,在A 分組的內(nèi)部在按照B來分組

-- 查詢各部門(10,20,30)內(nèi)各職位(PROGRAMER 2,CLERK 3,ANALIST 3,MANAGER 2,PESIDENT 1,SALESMAN 3)的平均工資

select * from emp

select deptno,job,AVG(sal)? from emp? GROUP BY deptno,job

-- 在 SQL 中增加 HAVING 子句原因是,WHERE 關(guān)鍵字無法與分組函數(shù)(5個)一起使用。

-- 例如 where avg(sal)>=2500 錯誤的寫法

? ? ? HAVING AVG(sal)>=2500 正確的寫法

-- 編寫SQL的語句的順序:select...from...where...group by(分組)...having ...order by (排序)

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 有分組函數(shù) 用 having(對沒有的加一起的列名提出的條件)

select 查詢條件 from 表名 where 表1,2之間相同的聯(lián)系 group by(分組)...having (對沒有的加一起的列名提出的條件)order by (排序)

select deptno,avg(sal) from emp group by deptno having AVG(sal)>=2500

select* from student;

select* from grade;

-- 查詢1號同學的姓名和總成績

-- 1. 用幾張表(student,grade)

-- 2. 查詢那些列 (sname,sum(sqrade) )

-- 3. 是否使用分組(group by) 根據(jù)sname 來分組

-- 4. where (1號同學)

select s.sname,sum(g.sgrade)

from student s,grade g?

where? s.sid = g.sid and s.sid=1

group by s.sname

select s.sname,sum(g.sgrade)

from student s INNER JOIN grade g?

on s.sid = g.sid where s.sid=1

group by s.sname

-- 查詢一班學生的學號 姓名 總成績

select s.sname,s.sid,sum(g.sgrade)

from student s,grade g

where? s.sid=g.sid and s.sclass='一班'

group by s.sid,s.sname

-- 查詢平均分大于85,總分大于170的學生的學號 姓名 和班級

select* from student;

select* from grade;

select s.sid,s.sname,s.sclass,avg(g.sgrade),sum(g.sgrade)

from student s ,grade g

where s.sid=g.sid

group by s.sid,s.sname,s.sclass

HAVING avg(g.sgrade)>85 and sum(g.sgrade)>170

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容