使用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)