🏝?專欄:Mysql_貓咪-9527的博客-CSDN博客
🌅主頁:貓咪-9527-CSDN博客?“欲窮千里目,更上一層樓。會當凌絕頂,一覽眾山小。”
目錄
7.函數
7.1 日期函數
函數總:?編輯
獲得當前日期
獲得當前時間
獲得時間戳
在日期的基礎上加日期
在日期的基礎上減去日期
計算兩個日期之間相差多少天
案例1:
案例二:
7.2 字符串函數
函數總:
獲取表中列的字符集
格式化字符串
查找字符串所出現的位置
計算字符串字節長度
替換字符串中的字符
截取字符串的一部分
轉換大小寫
刪除空格?
7.3 數學函數
絕對值
向上取整
向下取整
四舍五入保留小數位
產生隨機數
7.4 其它函數
查詢當前用戶
MD5 摘要
顯示當前數據庫
密碼加密
判斷是否為 NULL
7.函數
7.1 日期函數
函數總:
-
獲得當前日期
select current_date();
返回當前日期,格式為YYYY-MM-DD
。
select current_date();
-
獲得當前時間
select current_time();
返回當前時間,格式為HH:MM:SS
。
select current_time();
select current_date();
select current_time();
-
獲得時間戳
select current_timestamp();
返回當前的日期和時間,格式為YYYY-MM-DD HH:MM:SS
。
select current_timestamp();
select now();
select current_timestamp();
select now();
-
在日期的基礎上加日期
select date_add('2017-10-28', interval 10 day);
將日期2017-10-28
加上 10 天,返回結果為2017-11-07
。
select date_add('2025-3-24',interval 17 day);
-
在日期的基礎上減去日期
select date_sub('2017-10-1', interval 2 day);
將日期2017-10-01
減去 2 天,返回結果為2017-09-29
。
select date_sub(now(),interval 10 day);
select date_add('2025-3-24',interval 17 day);
select date_sub(now(),interval 10 day);
-
計算兩個日期之間相差多少天
select datediff('2017-10-10', '2016-9-1');
計算兩個日期之間的差值,返回結果為404
天。
select datediff('2025-2-24',now());
案例1:
創建一個生日表
create table birthday(
id int primary key auto_increment,
birthday date);
添加當前時間為生日:
insert birthday(birthday) values(current_date());
案例二:
創建一個評論表:
create table comments(
id int primary key auto_increment,
commtent varchar(200),
release_time datetime
);
?插入評論:
insert comments(commtent,release_time) values('千金散盡還復來',now());
?查找兩分鐘之前的評論:
7.2 字符串函數
函數總:
-
獲取表中列的字符集
select charset(ename) from EMP;
返回ename
列的字符集。
select charset(ename) from emp;
-
格式化字符串
select concat(name, '的語文是', chinese, '分,數學是', math, '分') as '分數' from exam_result;
將學生的成績按照指定格式輸出。
select concat(name,'的數學成績是:',math,'的語文成績是:',
chinese,',英語成績是:',english) from exam_result;
-
查找字符串所出現的位置
- select instr(string,substring);查看substring在string中所出現的位置,成功返回第幾個字符,失敗返回0
| |
![]() | ![]() |
-
計算字符串字節長度
select length(name), name from exam_result;
獲取name
字段的字節長度(根據字符集不同,中文可能占多個字節)。
select name,length(name) from exam_result;
注:一個漢字在utf_8中占據3個字節
-
替換字符串中的字符
select replace(ename, 'S', '上海') ,ename from EMP;
將ename
字段中的S
替換為上海
。
select replace(job,'S','上海')from emp;
-
截取字符串的一部分
select substring(ename, 2, 2), ename from EMP;
截取ename
字段從第二個字符開始的兩個字符。
select substring('string',2,2);
-
轉換大小寫
- ucase(string),將string全部變為大寫
- lcase(string),將string全部變為小寫
| |
![]() | ![]() |
-
刪除空格?
- ltrim(string)?刪除string左邊的空格
- rtrim(string)刪除string右邊的空格
- trim(string)刪除string左右兩邊的空格
| |
![]() | ![]() |
| |
![]() | ![]() |
7.3 數學函數
-
絕對值
select abs(-100.2);
返回 100.2,表示絕對值。
| | |
![]() | ![]() | ![]() |
-
向上取整
select ceiling(23.04);
返回 24,表示向上取整。
| | |
![]() | ![]() | ![]() |
-
向下取整
select floor(23.7);
返回 23,表示向下取整。
| | |
![]() | ![]() | ![]() |
-
四舍五入保留小數位
select format(12.3456, 2);
返回12.35
,保留 2 位小數。
| | |
![]() | ![]() | ![]() |
-
產生隨機數
select rand();
返回一個 0 到 1 之間的隨機浮動數值。
select rand();
生成0到99的隨機數?
?
7.4 其它函數
-
查詢當前用戶
select user();
返回當前數據庫用戶的信息。
-
MD5 摘要
select md5('admin');
對字符串'admin'
進行 MD5 加密,返回加密后的結果。
-
顯示當前數據庫
select database();
返回當前正在使用的數據庫名稱。
-
密碼加密
select password('root');
對'root'
進行加密,返回加密后的結果。
-
判斷是否為 NULL
select ifnull('abc', '123');
如果第一個參數為NULL
,則返回第二個參數。否則返回第一個參數。