數學函數的使用
常用數學函數
函數 |
作用 |
函數 |
作用 |
ceil() |
進一取整 |
abs() |
取絕對值 |
floor() |
舍掉小數部分 |
power() |
冪運算 |
round() |
四捨五入 |
pi() |
圓周率 |
truncate() |
截取小數點後幾位 |
rand()或者rand(x) |
0~1之間的隨機數 |
mod |
取餘數 |
sign(x) |
得到數字符號 |
exp() |
計算e的x次方 |
|
|
用法
select ceil(1.2);
select floor(2.9);
select round(3.56789,2);
select truncate(3.456789,3);
select 10 mod 3;
select id,username,ceil(salary) from user;
字符串常用函數
select char_length('abc');
select length('abc');
select concat('a','b','c');
select concat_ws('-','a','b','c',null);
select concat_ws(null,'a','b','c');
select upper('hello world'),ucase('hello world'),lower('HELLO WORLD'),lcase('HELLO WORLD');
select reverse('abc');
select left('hello',2),right('hello',2);
select lpad('abc',10,'?');
select concat('*',trim(' abc '),'*');
select repeat('hello',3);
select replace('hello king','king','queen');
select substring('abcdef',1,3);
select strcmp('a','b');
日期時間常用函數的使用
select curdate(),current_date();
select curtime(),current_time();
select now(),current_timestamp(),sysdate();
select month('2017-02-19');
select month(current_date()),monthname(curdate());
select dayname(now());
select dayofweek(now());
select week(now());
select year(now()),month(now()),day(now()),hour(now()),minute(now()),second(now());
select datediff('2017-03-05','2017-03-01');
其他常用函數
select version(),connection_id();
select database(),schema();
select user(),current_user(),system_user(),session_user();
select last_insert_id();
select md5('king');
select password('root');