使用MySQL特有函數:
日期函數:
ADDTIME (date2 ,time_interval ) |
將time_interval加到date2 |
CURRENT_DATE ( ) |
當前日期 |
CURRENT_TIME ( ) |
當前時間 |
CURRENT_TIMESTAMP ( ) |
當前時間戳 |
DATE (datetime ) |
返回datetime的日期部分 |
DATE_ADD (date2 , INTERVAL d_value d_type ) |
在date2中加上日期或時間 |
DATE_SUB (date2 , INTERVAL d_value d_type ) |
在date2上減去一個時間 |
DATEDIFF (date1 ,date2 ) |
兩個日期差 |
NOW ( ) |
當前時間 |
YEAR|Month|DAY (datetime ) |
年月日 |
字符串函數:
CHARSET(str) |
返回字串字符集 |
CONCAT (string2 [,... ]) |
連接字串 |
INSTR (string ,substring ) |
返回substring在string中出現的位置,沒有返回0 |
UCASE (string2 ) |
轉換成大寫 |
LCASE (string2 ) |
轉換成小寫 |
LEFT (string2 ,length ) |
從string2中的左邊起取length個字符 |
LENGTH (string ) |
string長度 |
REPLACE (str ,search_str ,replace_str ) |
在str中用replace_str替換search_str |
STRCMP (string1 ,string2 ) |
逐字符比較兩字串大小, |
SUBSTRING (str , position [,length ]) |
從str的position開始,取length個字符 |
LTRIM (string2 ) RTRIM (string2 ) trim() |
去除前端空格或後端空格 |
數學函數:
ABS (number2 ) |
絕對值 |
BIN (decimal_number ) |
十進制轉二進制 |
CEILING (number2 ) |
向上取整 |
CONV(number2,from_base,to_base) |
進制轉換 |
FLOOR (number2 ) |
向下取整 |
FORMAT (number,decimal_places ) |
保留小數位數 |
HEX (DecimalNumber ) |
轉十六進制 |
LEAST (number , number2 [,..]) |
求最小值 |
MOD (numerator ,denominator ) |
求餘 |
RAND([seed]) |
RAND([seed]) |
函數的使用:
l select now();
l select year(now());
l select month(now());
l select day(now());
l select floor(datediff(now(),‘1999-01-01’)/365);//間隔年
l select format(rand(),2);
l select floor(rand()*5)+1;[1-5]隨機值
l select length(trim(' jack '));
l select strcmp('a','w');
--到年底還有幾少天?
mysql> selectdatediff('2013-12-31',now());
+------------------------------+
| datediff('2013-12-31',now())|
+------------------------------+
| 51 |
+------------------------------+
1 row in set (0.10 sec)
mysql> select now();
+---------------------+
| now() |
+---------------------+
| 2013-11-10 11:11:54 |
+---------------------+
1 row in set(0.00 sec)
--取當前日期的年月日
mysql> selectyear(now()),month(now()),day(now());
+-------------+--------------+------------+
| year(now()) | month(now()) | day(now()) |
+-------------+--------------+------------+
| 2013 | 11 | 10 |
+-------------+--------------+------------+
1 row in set (0.00 sec)
--截取字符串
--selectsubstring('mysql',1,2); //從1開始
mysql> selectsubstring('mysql',1,2);
+------------------------+
| substring('mysql',1,2) |
+------------------------+
| my |
+------------------------+
1 row in set (0.00 sec)
--保留小數點後2位(四捨五入) selectformat(number,n)l number爲數字,n爲位數
--selectformat(3.1415926535657989,3);
mysql> selectformat(3.1415926535657989,3);
+------------------------------+
| format(3.1415926535657989,3) |
+------------------------------+
| 3.142 |
+------------------------------+
1 row in set (0.01 sec)
--向下取整(截取)select floor(num) 取整 num爲數字
mysql> selectfloor(3.14);
+-------------+
| floor(3.14) |
+-------------+
| 3 |
+-------------+
1 row in set (0.00 sec)
mysql> selectfloor(-3.14);
+--------------+
| floor(-3.14) |
+--------------+
| -4 |
+--------------+
1 row in set (0.06 sec)select floor(3.54);
mysql> selectfloor(-3.54);
+--------------+
| floor(-3.54) |
+--------------+
| -4 |
+--------------+
1 row in set (0.00 sec)
--取隨機值 select format(rand(),num); rand()隨機值,num保留的位數,
selectformat(rand(),2);
mysql> selectformat(rand(),2);
+------------------+
| format(rand(),2) |
+------------------+
| 0.02 |
+------------------+
1 row in set (0.02 sec)
--取1-6之間的隨機整數值
--selectfloor(rand()*6) + 1;
mysql> selectfloor(rand()*6) + 1;
+---------------------+
| floor(rand()*6) + 1 |
+---------------------+
| 1 |
+---------------------+
1 row in set (0.00 sec)
mysql> selectfloor(rand()*6) + 1;
+---------------------+
| floor(rand()*6) + 1 |
+---------------------+
| 3 |
+---------------------+
1 row in set (0.00 sec)
mysql> selectfloor(rand()*6) + 1;
+---------------------+
| floor(rand()*6) + 1 |
+---------------------+
| 3 |
+---------------------+
1 row in set (0.00 sec)
--隨機產生'a'-'z'之間的隨機字符
(1)查詢'a'-'z'對應的Unicode值
select ascii('a');//97
mysql> select ascii('a');
+------------+
| ascii('a') |
+------------+
| 97 |
+------------+
1 row in set (0.00 sec)
selectascii('z');//122
mysql> select ascii('z');
+------------+
| ascii('z') |
+------------+
| 122 |
+------------+
1 row in set (0.00 sec)
(2)產生97-122之間的隨機整數
select floor(rand()*26)+97;
(3)產生97-122對應的字符
select char(floor(rand()*26)+97);
mysql> selectchar(floor(rand()*26)+97);
+---------------------------+
| char(floor(rand()*26)+97) |
+---------------------------+
| s |
+---------------------------+
1 row in set (0.00 sec)
--利用MySQL的函數:對密碼'123456'進行MD5加密。
select md5('123456');
mysql> selectmd5('123456');
+----------------------------------+
| md5('123456') |
+----------------------------------+
| e10adc3949ba59abbe56e057f20f883e |
+----------------------------------+
1 row in set (0.00 sec)
==========================流程控制函數=================================
MySQL特有流程控制函數
drop table user;
create table user(
idint primary key auto_increment,
namevarchar(20),
gender varchar(6),
salary float
);
insert into user(name,gender,salary)values('jack','male',4000);
insert into user(name,gender,salary)values('marry','female',5000);
insert into user(name,gender,salary)values('jim','male',6000);
insert into user(name,gender,salary)values('tom','male',7000);
insert into user(name,gender,salary)values('soso','female',NULL);
insert into user(name,gender,salary)values('haha','female',3500);
insert into user(name,gender,salary)values('hehe','female',4500);
select * from user;
mysql> create table user(
-> id int primary keyauto_increment,
-> name varchar(20),
-> gender varchar(6),
-> salary float
-> );
Query OK, 0 rows affected (0.66 sec)
mysql> insert intouser(name,gender,salary) values('jack','male',4000);
Query OK, 1 row affected (0.21 sec)
mysql> insert into user(name,gender,salary)values('marry','female',5000)
Query OK, 1 row affected (0.08 sec)
mysql> insert intouser(name,gender,salary) values('jim','male',6000);
Query OK, 1 row affected (0.17 sec)
mysql> insert intouser(name,gender,salary) values('tom','male',7000);
Query OK, 1 row affected (0.13 sec)
mysql> insert intouser(name,gender,salary) values('soso','female',NULL);
Query OK, 1 row affected (0.19 sec)
mysql> insert intouser(name,gender,salary) values('haha','female',3500);
Query OK, 1 row affected (0.13 sec)
mysql> insert intouser(name,gender,salary) values('hehe','female',4500);
Query OK, 1 row affected (0.20 sec)
:
1) if(value,第一值,第二值);
value爲真,取第一值,否則取第二值
將5000元(含)以上的員工標識爲"高薪",否則標識爲"起薪"
類似於Java中的三目運算符
selectif(salary>=5000,'高薪','起薪')
from user;
mysql> selectif(salary>=5000,'高薪','起薪')
-> from user;
+------------------------------------+
| if(salary>=5000,'高薪','起薪') |
+------------------------------------+
| 起薪 |
| 高薪 |
| 高薪 |
| 高薪 |
| 起薪 |
| 起薪 |
| 起薪 |
+------------------------------------+
7 rows in set (0.00 sec)
2)ifnull(value1,value2)
value1爲NULL,用value2替代
將薪水爲NULL的員工標識爲"無薪"
select name as 員工,ifnull(salary,'無薪')as 薪水情況
from user;
mysql> select nameas 員工,ifnull(salary,'無薪') as 薪水情況
-> from user;
+--------+--------------+
| 員工 | 薪水情況 |
+--------+--------------+
| jack | 4000 |
| marry | 5000 |
| jim | 6000 |
| tom | 7000 |
| soso | 無薪 |
| haha | 3500 |
| hehe | 4500 |
+--------+--------------+
7 rows in set (0.00 sec)
3) case when [value]then [result1] else [result2] end;
當value表達式的值爲true時,取result1的值,否則取result2的值(if...else...)
將5000元(含)以上的員工標識爲"高薪",否則標識爲"起薪"
select
case when salary>=5000 then '高薪'
else '起薪' end
from user;
mysql> select
-> case when salary>=5000 then '高薪'
-> else '起薪' end
-> from user;
+----------------------------------------------------------------+
| case when salary>=5000 then '高薪'
else '起薪' end |
+----------------------------------------------------------------+
| 起薪 |
| 高薪 |
| 高薪 |
| 高薪 |
| 起薪 |
| 起薪 |
| 起薪 |
+----------------------------------------------------------------+
7 rows in set (0.00 sec)
4) case [express]when [value1] then [result1] when [value2] then [result2] else [result3] end;
當express滿足value1時,取result1的值,滿足value2時,取result2的值,否則取result3的值(switch...case..)
將7000元的員工標識爲"高薪",6000元的員工標識爲"中薪",5000元則標識爲"起薪",否則標識爲"低薪"
select
case salary
when 7000 then '高薪'
when 6000 then '中薪'
when 5000 then '起薪'
else '低薪'end
from user;
mysql> select
-> case salary
-> when 7000 then '高薪'
-> when 6000 then '中薪'
-> when 5000 then '起薪'
-> else '低薪' end
-> from user;
+----------------------------------------------------------------------------------------------------------------+
| case salary
when 7000 then '高薪'
when 6000 then '中薪'
when 5000 then '起薪'
else '低薪' end |
+----------------------------------------------------------------------------------------------------------------+
| 低薪 |
| 起薪 |
| 中薪 |
| 高薪 |
| 低薪 |
| 低薪 |
| 低薪 |
+----------------------------------------------------------------------------------------------------------------+
7 rows in set (0.00 sec)
練習:
1)查詢相同性別的員工總人數>2的工資總和,並按工資總和降序排列
select count(*) as 員人數,genderas 性別,sum(salary) as 工資和
from user
group by gender
having count(*)>2
orderby sum(salary) desc;
mysql> select count(*) as 員人數,genderas 性別,sum(salary) as 工資和
-> from user
-> group by gender
-> having count(*)>2
-> order by sum(salary) desc;
+-----------+--------+-----------+
| 員人數 | 性別 | 工資和 |
+-----------+--------+-----------+
| 3 | male | 17000 |
| 4 | female | 13000 |
+-----------+--------+-----------+
2 rows in set (0.00 sec)
2)將性別爲男的員工工資-1000,性別爲女的員工工資+1000,在一條SQL上完成
selectif(gender='female',salary+1000,salary-1000) as 工資 from user;
mysql> selectif(gender='female',salary+1000,salary-1000) as 工資 from user;
+--------+
| 工資 |
+--------+
| 3000 |
| 6000 |
| 5000 |
| 6000 |
| NULL |
| 4500 |
| 5500 |
+--------+
7 rows in set (0.00 sec)