單行函數
對每一條記錄輸入值進行計算,得到相應的計算結果,返回給用戶,也就是說,每條記錄作為一個輸入參數,經過函數計算得到每條記錄的計算結果。
每一個函數中都有一些常用的函數(方法)
在學習函數的時候,Mysql數據庫為咱們提供了一張虛擬表 dual
字符函數:對字符串操作 (相當于JAVA中的String)
① Upper(字符串)---將字符串轉換為大寫例子:select upper('zhangsan') from dual;LOWER(字符串)---將字符串轉換為小寫例子:select ename,lower(ename) from emp;②LENGTH(字符串)-----獲取字符串的長度例子:select length('abcde') from dual;③REPALCE(字符串,舊的字符串,新的字符串)??? 替換例子:找到emp表中的7369人員的名字,進行替換select ename,REPLACE(ename,'TH','AA')from empwhere empno=7369;④SUBSTR(字符串,開始位置,長度)???? 截取例子:select ename,substr(ename,2,2)from empwhere empno=7369;例子:截取后三位select ename,substr(ename,-3)from empwhere empno=7369;⑤LPAD(字符串,字符串長度len,添加的字符串strs)? 動態拼接字符串,從左邊例子:select LPAD('he',10,'*') from dual;RPAD(字符串,字符串長度len,添加的字符串strs)? 動態拼接字符串,從右邊例子:select RPAD('he',10,'*') from dual;⑥trim 消除空格LTRIM(字符串)消除前面的空格例子:select? LTRIM('???? HELLO') from dual;RTRIM(字符串)消除后面的空格例子:select? RTRIM('???? HELLO???? ') from dual;
2、數值函數:對數字進行操作
①四舍五入 ROUND(數字,[保留的小數位數])例子:select ROUND(129.56) from dual;select ROUND(129.56,1) from dual;②求余數 ?????? MOD(數字1,數字2)例子:select MOD(11,3) FROM DUAL;③絕對值?? ABS(數字)例子:select ABS(-11) from dual;④返回不大于數字的最大整數 FLOOR(數字)例子:select floor(5.5) from dual;select floor(-5.5) from dual;⑤返回大于數字的最小整數? ceiling(數字)? --正負數都可以例子:select ceiling(5.5) from dual;select ceiling(-5.5) from dual;
3、日期函數:操作數據庫中的日期
①獲取當前時間now()sysdate()--區別:now獲取最開始函數的值--select now(),sleep(3),now() from dual;sysdate獲取函數執行時的值--select sysdate(),sleep(3),sysdate() from dual;②獲取日期 curdate()例子:select curdate() from dual;③獲取時間 curtime()例子:select curtime() from dual;④獲取星期 dayofweek(日期)? 返回索引例子:select dayofweek('2018-08-30') from dual;日期的工作日索引 1=星期日,2-星期一...7=星期六⑤獲取一月中的第幾天 dayofmonth(日期)例子:select DAYOFMONTH('2018-08-23') from dual;⑥獲取一年中的第幾天 dayofyear(日期)例子:select dayofyear('2018-08-30') from dual;⑦獲取日期中的月份? month(日期)例子:select month('2018-08-30') from dual;⑧獲取季度 quarter(日期)例子:select quarter('2018-08-30') from dual;
4、其它函數
①獲取當前數據庫 database()例子:select database() from dual;②獲取當前數據庫用戶 system_user()例子:select system_user() from dual;③獲取數據庫版本 version()例子:select version() from dual;④加密? md5(內容)例子:select md5('123456') from dual;⑤格式化 format(內容,格式化)例子:select format(123456789,3) from dual;