在mysql中有許多內置的函數,雖然功能都能在PHP代碼中實現,但巧妙的應用mysql內置函數可以大大的簡化開發過程,提高效率。
在這里我總結一下一些常用的,方便以后查看:
mysql字符串函數:
concat(String2 [,...]) //連接字串
lcase(string2) //轉換成小寫
ucase(string2) //轉換成大寫
length(string) //string長度
ltrim(string2) //去除前端空格
rtrim(string2) //去除后端空格
repeat(string2,count) //重復count次
replace(str,search_str,replace_str) //在str中用replace_str替換search_str
substring(str,position [,length]) //position開始,取length個字符,注意:position偏移量是從1開始的
space(count) //生成count個空格
mysql數學函數:
bin(decimal_number) //十進制數字轉換成二進制
ceiling(number2)//向上取整
floor(number2)//向下取整
max(列名)//最大值
min(列名)//最小值
sqrt(number)//平方根
rand()//隨機數(0-1),可以進行隨機排序:select * from user order by rand();
mysql日期函數:
curdate()// 返回當前日期
curtime()//返回當前時間
now()//返回當前的日期時間
unix_timestamp(date)//返回當前date的unix時間戳
from_unixtime(時間戳)//返回UNIX時間戳的日期值
week(date)//返回日期date為一年中的第幾周
year(date)//返回日期date的年份
datediff(expr,expr2)//返回起始時間expr和結束時間expr2間的天數