16. mysql常用函數-字元串函數-數字函數-日期函數-進階函數
函數
描述
執行個體
char_length(s)
傳回字元串 s 的字元數
select char_length('lijw') as '長度';
concat(s1,s2...sn)
字元串 s1,s2 等多個字元串合并為一個字元串
select concat('happy','niu','year');
lower(s)
将字元串 s 的所有字母變成小寫字母
select lower('happy niu year');
upper(s)
将字元串轉換為大寫
select upper("happy niu year");
substr(s, start,length)
從字元串 s 的 start 位置截取長度為 length 的子字元串
select substr("happy niu year",1,2);
trim(s)
去掉字元串 s 開始和結尾處的空格
select trim(' happy niu year ')
示例:
rand()
傳回 0 到 1 的随機數
select rand();
round(小 小數 數, 保留 幾位 位)
四舍五入保留幾位小數
select round(3.1415926,2) ;
least(expr1, expr2, expr3, ...)
傳回清單中的最小值
select least(13, 14, 521, 74, 1)
greatest(expr1, expr2,expr3, ...)
傳回清單中的最大值
select greatest(13, 14, 521, 74, 1)
函數名
now() 和 sysdate()
傳回系統的目前日期和時間
select now(); 或 select sysdate();
curdate()
傳回目前日期
select curdate();
curtime()
傳回目前系統時間
select curtime();
year(d)
傳回d的中的年份
select year(now());
month(d)
傳回d的中的月份
select month(now());
day(d)
傳回d中的日
select day(now());
current_user()
傳回目前使用者
select current_user();
ifnull(v1,v2)
如果 v1 的值不為 null,則傳回 v1,否則傳回 v2。
select ifnull(null,'hello word')
isnull(expression)
判斷表達式是否為 null
select isnull(null);