- 浏览: 201473 次
- 性别:
- 来自: 上海
文章分类
最新评论
-
qiankai86:
s
多个文件上传的功能 -
zhjxzhj:
经测试不能用
PDF破解软件 -
meadlai:
很不错...哈哈...
PDF破解软件 -
talin2010:
刚学了,复习一下。。
Mysql+tomcat连接池自己的例子 -
yshuaiwen:
上面的方法都不怎么好,太麻烦,而且都需要改tomcat的xml ...
Mysql+tomcat连接池的配置实例
MYSQL函数
mysql> select round(10.33),round(64.50001),round(10.98547);
+--------------+-----------------+-----------------+
| round(10.33) | round(64.50001) | round(10.98547) |
+--------------+-----------------+-----------------+
| 10 | 65 | 11 |
+--------------+-----------------+-----------------+
1 row in set (0.00 sec)
Round 函数四舍五入
mysql> select truncate(1.78502356,3),truncate(-6235.3256984,10);
+------------------------+----------------------------+
| truncate(1.78502356,3) | truncate(-6235.3256984,10) |
+------------------------+----------------------------+
| 1.785 | -6235.3256984000 |
+------------------------+----------------------------+
1 row in set (0.00 sec)
mysql> select truncate(1789.09854,-2),truncate(-629.6953245,-1);
+-------------------------+---------------------------+
| truncate(1789.09854,-2) | truncate(-629.6953245,-1) |
+-------------------------+---------------------------+
| 1700 | -620 |
+-------------------------+---------------------------+
1 row in set (0.00 sec)
Truncate 把一个数字截短成为一个指定小数个数的数字,注意取负数的时候哦
mysql> select abs (-789);
+------------+
| abs (-789) |
+------------+
| 789 |
+------------+
1 row in set (0.00 sec)
Abs绝对值
mysql> select * from toys;
+----+-----------------+-------+-------+-------+----------+
| id | item | ctm | ctr | ctc | quantity |
+----+-----------------+-------+-------+-------+----------+
| 0 | ww | 3.00 | 3.00 | 3.00 | 200 |
| 1 | huggy bear | 5.00 | 7.40 | 9.90 | 300 |
| 2 | coloring book | 3.00 | 5.00 | 7.99 | 2000 |
| 3 | board game | 12.00 | 20.00 | 39.90 | 1430 |
| 4 | activity cd-rom | 2.00 | 6.00 | 17.49 | 3780 |
| 6 | ww | 6.00 | 6.00 | 6.00 | 100 |
+----+-----------------+-------+-------+-------+----------+
6 rows in set (0.00 sec)
mysql> select * from toys group by item;
+----+-----------------+-------+-------+-------+----------+
| id | item | ctm | ctr | ctc | quantity |
+----+-----------------+-------+-------+-------+----------+
| 4 | activity cd-rom | 2.00 | 6.00 | 17.49 | 3780 |
| 3 | board game | 12.00 | 20.00 | 39.90 | 1430 |
| 2 | coloring book | 3.00 | 5.00 | 7.99 | 2000 |
| 1 | huggy bear | 5.00 | 7.40 | 9.90 | 300 |
| 0 | ww | 3.00 | 3.00 | 3.00 | 200 |
+----+-----------------+-------+-------+-------+----------+
5 rows in set (0.01 sec)
Group by 去掉重复的纪录
字符串函数
mysql> select length('123456');
+------------------+
| length('123456') |
+------------------+
| 6 |
+------------------+
1 row in set (0.00 sec)
mysql> select length('fsfsdsesdsfdfsd');
+---------------------------+
| length('fsfsdsesdsfdfsd') |
+---------------------------+
| 15 |
+---------------------------+
1 row in set (0.00 sec)
Length 返回字符串的长度
mysql> select rtrim("right hand "), ltrim(" left eye");
+--------------------------+----------------------+
| rtrim("right hand ") | ltrim(" left eye") |
+--------------------------+----------------------+
| right hand | left eye |
+--------------------------+----------------------+
1 row in set (0.00 sec)
Ltrim()去掉首部的空格
Rtrim()去掉尾部的空格
mysql> select trim(' res sere ');
+-----------------------+
| trim(' res sere ') |
+-----------------------+
| res sere |
+-----------------------+
Trim 去掉首尾的空格
mysql> select left('hello',4),right('welcome to shenyang',5);
+-----------------+--------------------------------+
| left('hello',4) | right('welcome to shenyang',5) |
+-----------------+--------------------------------+
| hell | nyang |
+-----------------+--------------------------------+
1 row in set (0.00 sec)
Left 和right 从左边或者右边返回字符
mysql> select left(right('market',5),3);
+---------------------------+
| left(right('market',5),3) |
+---------------------------+
| ark |
+---------------------------+
1 row in set (0.00 sec)
取得中间的部分
mysql> select substring('market',2,3);
+-------------------------+
| substring('market',2,3) |
+-------------------------+
| ark |
+-------------------------+
1 row in set (0.00 sec)
Substring 也是可以的
mysql> select concat('sds','dsee');
+----------------------+
| concat('sds','dsee') |
+----------------------+
| sdsdsee |
+----------------------+
1 row in set (0.00 sec)
mysql> select concat('sds',"ee22");
+----------------------+
| concat('sds',"ee22") |
+----------------------+
| sdsee22 |
+----------------------+
1 row in set (0.00 sec)
mysql> select concat('sds',23);
+------------------+
| concat('sds',23) |
+------------------+
| sds23 |
+------------------+
1 row in set (0.00 sec)
Concat 连接字符串
mysql> select insert('21010419801010171*',18,1,'x');
+---------------------------------------+
| insert('21010419801010171*',18,1,'x') |
+---------------------------------------+
| 21010419801010171x |
+---------------------------------------+
1 row in set (0.00 sec)
mysql> select insert('I am Joe Cool',10,4,'camel');
+--------------------------------------+
| insert('I am Joe Cool',10,4,'camel') |
+--------------------------------------+
| I am Joe camel |
+--------------------------------------+
1 row in set (0.00 sec)
Insert 函数可以把一个字符串的指定部分(定义了起始位置和长度)替换为一个新值
mysql> select repeat('ha',5);
+----------------+
| repeat('ha',5) |
+----------------+
| hahahahaha |
+----------------+
1 row in set (0.05 sec)
Repeat函数用于重复字符串
mysql> select reverse(repeat('ha',3));
+-------------------------+
| reverse(repeat('ha',3)) |
+-------------------------+
| ahahah |
+-------------------------+
1 row in set (0.00 sec)
Reverse 函数颠倒字符串
日期和时间函数
mysql> select now();
+---------------------+
| now() |
+---------------------+
| 2008-10-15 16:44:00 |
+---------------------+
1 row in set (0.03 sec)
mysql> select curtime(),curdate();
+-----------+------------+
| curtime() | curdate() |
+-----------+------------+
| 16:44:29 | 2008-10-15 |
+-----------+------------+
1 row in set (0.02 sec)
mysql> select date_add('2004-01-01 00:00:00',interval 6 month);
+--------------------------------------------------+
| date_add('2004-01-01 00:00:00',interval 6 month) |
+--------------------------------------------------+
| 2004-07-01 00:00:00 |
+--------------------------------------------------+
1 row in set (0.00 sec)
第一个例子在初始时间加上6个月并返回结果
mysql> select date_add('2004-03-14 12:20:00',interval '12 03:45' day_minute);
+----------------------------------------------------------------+
| date_add('2004-03-14 12:20:00',interval '12 03:45' day_minute) |
+----------------------------------------------------------------+
| 2004-03-26 16:05:00 |
+----------------------------------------------------------------+
1 row in set (0.00 sec)
第二个例子向初始时间增加12天3小时45分钟,并显示结果
mysql> select date_sub('2004-01-01 07:00:00',interval 8 hour);
+-------------------------------------------------+
| date_sub('2004-01-01 07:00:00',interval 8 hour) |
+-------------------------------------------------+
| 2003-12-31 23:00:00 |
+-------------------------------------------------+
1 row in set (0.00 sec)
从一个日期值减去8个小时
mysql> select date_sub('2004-01-01 07:20:00',interval '13-4',year_month);
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near 'year_
month)' at line 1
日期值减去13年4个月 没有效果!!!!
P135
mysql> select extract(year from'1999-07-02');
+--------------------------------+
| extract(year from'1999-07-02') |
+--------------------------------+
| 1999 |
+--------------------------------+
1 row in set (0.00 sec)
mysql> select extract(year_month from'1999-07-02');
+--------------------------------------+
| extract(year_month from'1999-07-02') |
+--------------------------------------+
| 199907 |
+--------------------------------------+
1 row in set (0.00 sec)
Extract()函数获得一个日期的指定部分
mysql> select period_diff(200302,199802);
+----------------------------+
| period_diff(200302,199802) |
+----------------------------+
| 60 |
+----------------------------+
1 row in set (0.02 sec)
period_diff 函数返回两个日期之间的差值(月数)
mysql> select name,dob,round(period_diff(date_format(now(),'%y%m'),date_format(d
ob,'%y%m'))/12,1) as age from birthday;
+-------+---------------------+-------+
| name | dob | age |
+-------+---------------------+-------+
| raoul | 1978-06-04 00:00:00 | 30.3 |
| luis | 1965-11-17 00:00:00 | -57.1 |
| larry | 1971-08-19 00:00:00 | 37.2 |
| moe | 1992-01-23 00:00:00 | 16.8 |
+-------+---------------------+-------+
4 rows in set (0.02 sec)
为什么出来负数???
mysql> select name,dob,(to_days(now())-to_days(dob))/365 as age from birthday;
+-------+---------------------+---------+
| name | dob | age |
+-------+---------------------+---------+
| raoul | 1978-06-04 00:00:00 | 30.3890 |
| luis | 1965-11-17 00:00:00 | 42.9425 |
| larry | 1971-08-19 00:00:00 | 37.1863 |
| moe | 1992-01-23 00:00:00 | 16.7425 |
+-------+---------------------+---------+
4 rows in set (0.00 sec)
mysql> select member,to_days(check_in_date)-to_days(check_out_date) as out_perio
d from library;
+---------+------------+
| member | out_period |
+---------+------------+
| joe | 3 |
| john | 6 |
| johann | 4 |
| mark | 30 |
| jthomas | 8 |
+---------+------------+
5 rows in set (0.00 sec)
查询出来每个电影借出多长时间
mysql> select member from library where to_days(check_in_date)-to_days
-> (check_out_date)>4;
+---------+
| member |
+---------+
| john |
| mark |
| jthomas |
+---------+
3 rows in set (0.00 sec)
假设每部电影可以借出的最长时间是4天,找到哪个成员借出电影的时间过期了
加密函数(略)
控制流函数
mysql> select ifnull(1,2),ifnull(null,10),ifnull(4 * null,'false');
+-------------+-----------------+--------------------------+
| ifnull(1,2) | ifnull(null,10) | ifnull(4 * null,'false') |
+-------------+-----------------+--------------------------+
| 1 | 10 | false |
+-------------+-----------------+--------------------------+
1 row in set (0.00 sec)
Ifnull 函数,它有2个参数,并且对第一个参数进行判断。如果第一个参数不是null,函数就会向调用者返回第一个参数;如果是null,将返回第二个参数
mysql> select nullif(1,1),nullif('a','b'),nullif(2+3,4+1);
+-------------+-----------------+-----------------+
| nullif(1,1) | nullif('a','b') | nullif(2+3,4+1) |
+-------------+-----------------+-----------------+
| NULL | a | NULL |
+-------------+-----------------+-----------------+
1 row in set (0.02 sec)
Nullif 函数将会检验提供的两个参数是否相等,如果相等,则返回null,如果不等就返回第一个参数
mysql> select if(1<10,2,3),if(56>100,'ture','fasle');
+--------------+---------------------------+
| if(1<10,2,3) | if(56>100,'ture','fasle') |
+--------------+---------------------------+
| 2 | fasle |
+--------------+---------------------------+
1 row in set (0.00 sec)
If 函数可以建立一个简单的条件测试。它有三个参数。第一个是要被判断的表达式,如果表达式为真,if将会返回第二个参数 如果为假if 将会返回第三个参数
mysql> select case 'green'
-> when 'red' then 'stop'
-> when 'green' then 'go'
-> end;
+----------------------------------------------------------------+
| case 'green'
when 'red' then 'stop'
when 'green' then 'go'
end |
+----------------------------------------------------------------+
| go |
+----------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> select case 9
-> when 1 then 'a'
-> when 2 then 'b'
-> else 'n/a'
-> end;
+-------------------------------------------------------+
| case 9
when 1 then 'a'
when 2 then 'b'
else 'n/a'
end |
+-------------------------------------------------------+
| n/a |
+-------------------------------------------------------+
1 row in set (0.00 sec)
mysql> select case 2
-> when 1 then 'a'
-> when 2 then 'b'
-> else 'n/a'
-> end;
+-------------------------------------------------------+
| case 2
when 1 then 'a'
when 2 then 'b'
else 'n/a'
end |
+-------------------------------------------------------+
| b |
+-------------------------------------------------------+
1 row in set (0.00 sec)
mysql> select case
-> when (2+2)=4 then 'ok'
-> when (2+2)<>4 then 'nook'
-> end as status;
+--------+
| status |
+--------+
| ok |
+--------+
1 row in set (0.00 sec)
关于case 函数的用法
格式化函数
mysql> select format(99999999.69639,2), format(-4512,4);
+--------------------------+-----------------+
| format(99999999.69639,2) | format(-4512,4) |
+--------------------------+-----------------+
| 99,999,999.70 | -4,512.0000 |
+--------------------------+-----------------+
1 row in set (0.00 sec)
mysql> select date_format(now(),'%w,%d,%m,%y,%r');
+-------------------------------------+
| date_format(now(),'%w,%d,%m,%y,%r') |
+-------------------------------------+
| 4,16,10,08,10:40:24 AM |
+-------------------------------------+
1 row in set (0.03 sec)
mysql> select date_format(19980317,'%d/%m/%y');
+----------------------------------+
| date_format(19980317,'%d/%m/%y') |
+----------------------------------+
| 17/03/98 |
+----------------------------------+
1 row in set (0.00 sec)
类型转化函数
mysql> select cast(now() as signed integer),curdate()+0;
+-------------------------------+-------------+
| cast(now() as signed integer) | curdate()+0 |
+-------------------------------+-------------+
| 20081016105615 | 20081016 |
+-------------------------------+-------------+
1 row in set (0.03 sec)
有关cast 函数还需要找一些例子,在实际中比较有用的
mysql> select round(10.33),round(64.50001),round(10.98547);
+--------------+-----------------+-----------------+
| round(10.33) | round(64.50001) | round(10.98547) |
+--------------+-----------------+-----------------+
| 10 | 65 | 11 |
+--------------+-----------------+-----------------+
1 row in set (0.00 sec)
Round 函数四舍五入
mysql> select truncate(1.78502356,3),truncate(-6235.3256984,10);
+------------------------+----------------------------+
| truncate(1.78502356,3) | truncate(-6235.3256984,10) |
+------------------------+----------------------------+
| 1.785 | -6235.3256984000 |
+------------------------+----------------------------+
1 row in set (0.00 sec)
mysql> select truncate(1789.09854,-2),truncate(-629.6953245,-1);
+-------------------------+---------------------------+
| truncate(1789.09854,-2) | truncate(-629.6953245,-1) |
+-------------------------+---------------------------+
| 1700 | -620 |
+-------------------------+---------------------------+
1 row in set (0.00 sec)
Truncate 把一个数字截短成为一个指定小数个数的数字,注意取负数的时候哦
mysql> select abs (-789);
+------------+
| abs (-789) |
+------------+
| 789 |
+------------+
1 row in set (0.00 sec)
Abs绝对值
mysql> select * from toys;
+----+-----------------+-------+-------+-------+----------+
| id | item | ctm | ctr | ctc | quantity |
+----+-----------------+-------+-------+-------+----------+
| 0 | ww | 3.00 | 3.00 | 3.00 | 200 |
| 1 | huggy bear | 5.00 | 7.40 | 9.90 | 300 |
| 2 | coloring book | 3.00 | 5.00 | 7.99 | 2000 |
| 3 | board game | 12.00 | 20.00 | 39.90 | 1430 |
| 4 | activity cd-rom | 2.00 | 6.00 | 17.49 | 3780 |
| 6 | ww | 6.00 | 6.00 | 6.00 | 100 |
+----+-----------------+-------+-------+-------+----------+
6 rows in set (0.00 sec)
mysql> select * from toys group by item;
+----+-----------------+-------+-------+-------+----------+
| id | item | ctm | ctr | ctc | quantity |
+----+-----------------+-------+-------+-------+----------+
| 4 | activity cd-rom | 2.00 | 6.00 | 17.49 | 3780 |
| 3 | board game | 12.00 | 20.00 | 39.90 | 1430 |
| 2 | coloring book | 3.00 | 5.00 | 7.99 | 2000 |
| 1 | huggy bear | 5.00 | 7.40 | 9.90 | 300 |
| 0 | ww | 3.00 | 3.00 | 3.00 | 200 |
+----+-----------------+-------+-------+-------+----------+
5 rows in set (0.01 sec)
Group by 去掉重复的纪录
字符串函数
mysql> select length('123456');
+------------------+
| length('123456') |
+------------------+
| 6 |
+------------------+
1 row in set (0.00 sec)
mysql> select length('fsfsdsesdsfdfsd');
+---------------------------+
| length('fsfsdsesdsfdfsd') |
+---------------------------+
| 15 |
+---------------------------+
1 row in set (0.00 sec)
Length 返回字符串的长度
mysql> select rtrim("right hand "), ltrim(" left eye");
+--------------------------+----------------------+
| rtrim("right hand ") | ltrim(" left eye") |
+--------------------------+----------------------+
| right hand | left eye |
+--------------------------+----------------------+
1 row in set (0.00 sec)
Ltrim()去掉首部的空格
Rtrim()去掉尾部的空格
mysql> select trim(' res sere ');
+-----------------------+
| trim(' res sere ') |
+-----------------------+
| res sere |
+-----------------------+
Trim 去掉首尾的空格
mysql> select left('hello',4),right('welcome to shenyang',5);
+-----------------+--------------------------------+
| left('hello',4) | right('welcome to shenyang',5) |
+-----------------+--------------------------------+
| hell | nyang |
+-----------------+--------------------------------+
1 row in set (0.00 sec)
Left 和right 从左边或者右边返回字符
mysql> select left(right('market',5),3);
+---------------------------+
| left(right('market',5),3) |
+---------------------------+
| ark |
+---------------------------+
1 row in set (0.00 sec)
取得中间的部分
mysql> select substring('market',2,3);
+-------------------------+
| substring('market',2,3) |
+-------------------------+
| ark |
+-------------------------+
1 row in set (0.00 sec)
Substring 也是可以的
mysql> select concat('sds','dsee');
+----------------------+
| concat('sds','dsee') |
+----------------------+
| sdsdsee |
+----------------------+
1 row in set (0.00 sec)
mysql> select concat('sds',"ee22");
+----------------------+
| concat('sds',"ee22") |
+----------------------+
| sdsee22 |
+----------------------+
1 row in set (0.00 sec)
mysql> select concat('sds',23);
+------------------+
| concat('sds',23) |
+------------------+
| sds23 |
+------------------+
1 row in set (0.00 sec)
Concat 连接字符串
mysql> select insert('21010419801010171*',18,1,'x');
+---------------------------------------+
| insert('21010419801010171*',18,1,'x') |
+---------------------------------------+
| 21010419801010171x |
+---------------------------------------+
1 row in set (0.00 sec)
mysql> select insert('I am Joe Cool',10,4,'camel');
+--------------------------------------+
| insert('I am Joe Cool',10,4,'camel') |
+--------------------------------------+
| I am Joe camel |
+--------------------------------------+
1 row in set (0.00 sec)
Insert 函数可以把一个字符串的指定部分(定义了起始位置和长度)替换为一个新值
mysql> select repeat('ha',5);
+----------------+
| repeat('ha',5) |
+----------------+
| hahahahaha |
+----------------+
1 row in set (0.05 sec)
Repeat函数用于重复字符串
mysql> select reverse(repeat('ha',3));
+-------------------------+
| reverse(repeat('ha',3)) |
+-------------------------+
| ahahah |
+-------------------------+
1 row in set (0.00 sec)
Reverse 函数颠倒字符串
日期和时间函数
mysql> select now();
+---------------------+
| now() |
+---------------------+
| 2008-10-15 16:44:00 |
+---------------------+
1 row in set (0.03 sec)
mysql> select curtime(),curdate();
+-----------+------------+
| curtime() | curdate() |
+-----------+------------+
| 16:44:29 | 2008-10-15 |
+-----------+------------+
1 row in set (0.02 sec)
mysql> select date_add('2004-01-01 00:00:00',interval 6 month);
+--------------------------------------------------+
| date_add('2004-01-01 00:00:00',interval 6 month) |
+--------------------------------------------------+
| 2004-07-01 00:00:00 |
+--------------------------------------------------+
1 row in set (0.00 sec)
第一个例子在初始时间加上6个月并返回结果
mysql> select date_add('2004-03-14 12:20:00',interval '12 03:45' day_minute);
+----------------------------------------------------------------+
| date_add('2004-03-14 12:20:00',interval '12 03:45' day_minute) |
+----------------------------------------------------------------+
| 2004-03-26 16:05:00 |
+----------------------------------------------------------------+
1 row in set (0.00 sec)
第二个例子向初始时间增加12天3小时45分钟,并显示结果
mysql> select date_sub('2004-01-01 07:00:00',interval 8 hour);
+-------------------------------------------------+
| date_sub('2004-01-01 07:00:00',interval 8 hour) |
+-------------------------------------------------+
| 2003-12-31 23:00:00 |
+-------------------------------------------------+
1 row in set (0.00 sec)
从一个日期值减去8个小时
mysql> select date_sub('2004-01-01 07:20:00',interval '13-4',year_month);
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near 'year_
month)' at line 1
日期值减去13年4个月 没有效果!!!!
P135
mysql> select extract(year from'1999-07-02');
+--------------------------------+
| extract(year from'1999-07-02') |
+--------------------------------+
| 1999 |
+--------------------------------+
1 row in set (0.00 sec)
mysql> select extract(year_month from'1999-07-02');
+--------------------------------------+
| extract(year_month from'1999-07-02') |
+--------------------------------------+
| 199907 |
+--------------------------------------+
1 row in set (0.00 sec)
Extract()函数获得一个日期的指定部分
mysql> select period_diff(200302,199802);
+----------------------------+
| period_diff(200302,199802) |
+----------------------------+
| 60 |
+----------------------------+
1 row in set (0.02 sec)
period_diff 函数返回两个日期之间的差值(月数)
mysql> select name,dob,round(period_diff(date_format(now(),'%y%m'),date_format(d
ob,'%y%m'))/12,1) as age from birthday;
+-------+---------------------+-------+
| name | dob | age |
+-------+---------------------+-------+
| raoul | 1978-06-04 00:00:00 | 30.3 |
| luis | 1965-11-17 00:00:00 | -57.1 |
| larry | 1971-08-19 00:00:00 | 37.2 |
| moe | 1992-01-23 00:00:00 | 16.8 |
+-------+---------------------+-------+
4 rows in set (0.02 sec)
为什么出来负数???
mysql> select name,dob,(to_days(now())-to_days(dob))/365 as age from birthday;
+-------+---------------------+---------+
| name | dob | age |
+-------+---------------------+---------+
| raoul | 1978-06-04 00:00:00 | 30.3890 |
| luis | 1965-11-17 00:00:00 | 42.9425 |
| larry | 1971-08-19 00:00:00 | 37.1863 |
| moe | 1992-01-23 00:00:00 | 16.7425 |
+-------+---------------------+---------+
4 rows in set (0.00 sec)
mysql> select member,to_days(check_in_date)-to_days(check_out_date) as out_perio
d from library;
+---------+------------+
| member | out_period |
+---------+------------+
| joe | 3 |
| john | 6 |
| johann | 4 |
| mark | 30 |
| jthomas | 8 |
+---------+------------+
5 rows in set (0.00 sec)
查询出来每个电影借出多长时间
mysql> select member from library where to_days(check_in_date)-to_days
-> (check_out_date)>4;
+---------+
| member |
+---------+
| john |
| mark |
| jthomas |
+---------+
3 rows in set (0.00 sec)
假设每部电影可以借出的最长时间是4天,找到哪个成员借出电影的时间过期了
加密函数(略)
控制流函数
mysql> select ifnull(1,2),ifnull(null,10),ifnull(4 * null,'false');
+-------------+-----------------+--------------------------+
| ifnull(1,2) | ifnull(null,10) | ifnull(4 * null,'false') |
+-------------+-----------------+--------------------------+
| 1 | 10 | false |
+-------------+-----------------+--------------------------+
1 row in set (0.00 sec)
Ifnull 函数,它有2个参数,并且对第一个参数进行判断。如果第一个参数不是null,函数就会向调用者返回第一个参数;如果是null,将返回第二个参数
mysql> select nullif(1,1),nullif('a','b'),nullif(2+3,4+1);
+-------------+-----------------+-----------------+
| nullif(1,1) | nullif('a','b') | nullif(2+3,4+1) |
+-------------+-----------------+-----------------+
| NULL | a | NULL |
+-------------+-----------------+-----------------+
1 row in set (0.02 sec)
Nullif 函数将会检验提供的两个参数是否相等,如果相等,则返回null,如果不等就返回第一个参数
mysql> select if(1<10,2,3),if(56>100,'ture','fasle');
+--------------+---------------------------+
| if(1<10,2,3) | if(56>100,'ture','fasle') |
+--------------+---------------------------+
| 2 | fasle |
+--------------+---------------------------+
1 row in set (0.00 sec)
If 函数可以建立一个简单的条件测试。它有三个参数。第一个是要被判断的表达式,如果表达式为真,if将会返回第二个参数 如果为假if 将会返回第三个参数
mysql> select case 'green'
-> when 'red' then 'stop'
-> when 'green' then 'go'
-> end;
+----------------------------------------------------------------+
| case 'green'
when 'red' then 'stop'
when 'green' then 'go'
end |
+----------------------------------------------------------------+
| go |
+----------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> select case 9
-> when 1 then 'a'
-> when 2 then 'b'
-> else 'n/a'
-> end;
+-------------------------------------------------------+
| case 9
when 1 then 'a'
when 2 then 'b'
else 'n/a'
end |
+-------------------------------------------------------+
| n/a |
+-------------------------------------------------------+
1 row in set (0.00 sec)
mysql> select case 2
-> when 1 then 'a'
-> when 2 then 'b'
-> else 'n/a'
-> end;
+-------------------------------------------------------+
| case 2
when 1 then 'a'
when 2 then 'b'
else 'n/a'
end |
+-------------------------------------------------------+
| b |
+-------------------------------------------------------+
1 row in set (0.00 sec)
mysql> select case
-> when (2+2)=4 then 'ok'
-> when (2+2)<>4 then 'nook'
-> end as status;
+--------+
| status |
+--------+
| ok |
+--------+
1 row in set (0.00 sec)
关于case 函数的用法
格式化函数
mysql> select format(99999999.69639,2), format(-4512,4);
+--------------------------+-----------------+
| format(99999999.69639,2) | format(-4512,4) |
+--------------------------+-----------------+
| 99,999,999.70 | -4,512.0000 |
+--------------------------+-----------------+
1 row in set (0.00 sec)
mysql> select date_format(now(),'%w,%d,%m,%y,%r');
+-------------------------------------+
| date_format(now(),'%w,%d,%m,%y,%r') |
+-------------------------------------+
| 4,16,10,08,10:40:24 AM |
+-------------------------------------+
1 row in set (0.03 sec)
mysql> select date_format(19980317,'%d/%m/%y');
+----------------------------------+
| date_format(19980317,'%d/%m/%y') |
+----------------------------------+
| 17/03/98 |
+----------------------------------+
1 row in set (0.00 sec)
类型转化函数
mysql> select cast(now() as signed integer),curdate()+0;
+-------------------------------+-------------+
| cast(now() as signed integer) | curdate()+0 |
+-------------------------------+-------------+
| 20081016105615 | 20081016 |
+-------------------------------+-------------+
1 row in set (0.03 sec)
有关cast 函数还需要找一些例子,在实际中比较有用的
发表评论
-
MYSQL完全手册学习笔记(练习时候的数据库)
2008-10-23 11:14 2043MYSQL完全手册学习笔记(练习时候的数据库) 4-10章的 -
MYSQL完全手册学习笔记(第十章)
2008-10-23 11:05 1800连接 交叉连接 mysql> select * ... -
MYSQL完全手册学习笔记(第九章)
2008-10-21 17:51 1225使用数据 插入、更新 和删除记录 插入 Inser ... -
MYSQL完全手册学习笔记(第八章)
2008-10-20 11:30 2273使用数据库和表 创建数据库 mysql> crea ... -
MySQL 5.1参考手册
2008-10-15 15:49 1196http://dev.mysql.com/doc/refman ... -
MYSQL完全手册学习笔记(第六章)
2008-10-15 15:47 1159运算符 算数运算符 mysql> select 10+ ... -
MYSQL完全手册学习笔记(第五章)
2008-10-15 14:41 1151数据类型 Float(5,2)规定显示的值不会超过5位数字 ... -
MySQL 5.1新特性之事件调度器(Event Scheduler)
2008-10-15 14:39 1359MySQL 5.1新特性之事件调度器(Event Schedu ... -
MYSQL完全手册学习笔记(第四章)
2008-10-15 13:45 1266MYSQL完全手册学习笔记 ... -
MySQL常用命令及基本操作总结
2008-10-15 10:46 1850MySQL常用操作基本操作,以下都是MySQL5.0下测试通过 ... -
Mysql命令集锦
2008-09-04 17:45 861Mysql命令集锦 MySQL常 ... -
mysql 驱动文件
2008-08-26 10:04 1538mysql 驱动文件 -
mysql帮助文档
2008-08-26 10:00 2418mysql 的帮助文档说明 -
Mysql命令集锦
2008-08-23 09:32 1008Mysql命令集锦 MySQL常 ...
相关推荐
MYSQL完全手册MYSQL完全手册MYSQL完全手册MYSQL完全手册MYSQL完全手册
《MYSQL完全手册学习笔记》是针对MySQL数据库系统的一份详细学习资料,主要涵盖了数据库的各个方面,包括基础概念、安装配置、SQL语法、表的设计、索引、事务处理、视图、存储过程、触发器、备份恢复等。这些知识点...
### MySQL核心技术学习笔记 #### 一、为什么要学习数据库 学习数据库的重要性主要体现在以下几个方面: 1. **持久化数据到本地**:数据库能够将应用程序产生的数据持久化存储在磁盘上,即使系统重启也不会丢失...
MySQL是世界上最受欢迎的关系型数据库管理系统之一,尤其在Web应用...学习MySQL不仅涉及理论知识,还需要大量的实践操作来熟悉其语法和功能。掌握MySQL能让你在数据管理、Web开发和系统集成等领域具备更强的竞争力。
MySQL中文完全手册,扫描版,PDF版,自己逐个加了目录,绝壁比市面上别的文档要好用!
《MySQL完全手册(中文)》是MySQL数据库管理系统的一部详尽参考指南,旨在为读者提供全面、深入的MySQL知识。本书基于《MySQL: The Complete Reference》的中文翻译,覆盖了MySQL的所有核心概念、功能和最佳实践,...
数据库 MySQL 学习笔记高级篇.md
MySQL学习笔记.chmMySQL学习笔记.chmMySQL学习笔记.chmMySQL学习笔记.chm
美河提供.MYSQL完全手册 美河提供.MYSQL完全手册 美河提供.MYSQL完全手册 美河提供.MYSQL完全手册
mysql的完全参考手册,电子书籍,共1846页,可以说完全囊括了mysql的全部奥秘。打包成了两部分,请下载后一起解压缩。这是第二部份
MySQL OCP 超详细学习笔记$pdf MySQL OCP 超详细学习笔记.pdf 是一份详细的 MySQL 学习笔记,旨在帮助 MySQL DBA master 数据库管理的知识和技能。本笔记涵盖了 MySQL 的多个方面,包括 MySQL 的配置、性能优化、...
MySQL 高可用学习笔记 mysqlrouter_MHA 本文档主要讲解了 MySQL Router 和 MHA 的高可用配置和测试步骤。MySQL Router 是一个提供高可用和负载均衡的工具,而 MHA 是一个提供高可用和自动failover 的工具。下面我们...
这份"mysql完全手册"旨在为学习MySQL的用户提供全面的参考指南,涵盖从基础概念到高级特性的各种操作和查询。 手册可能会包括以下几个主要部分: 1. **MySQL安装与配置**:这部分会介绍如何在不同操作系统上安装...
这份“MySQL学习笔记”将引导我们深入理解其核心概念和实用技能。 一、MySQL简介 MySQL是一个开源、免费的数据库系统,由瑞典的MySQL AB公司开发,后被Oracle公司收购。它的设计目标是速度、可移植性和简洁性,支持...
MySQL优化学习思维笔记.xmind
这份“MySQL个人学习笔记”涵盖了从基础到进阶的多个方面,旨在帮助学习者全面理解MySQL的使用。 1. **安装与配置**: 在开始学习MySQL之前,你需要了解如何在不同的操作系统上安装MySQL服务器,如Windows、Linux...
这份"超经典mysql DBA学习笔记"涵盖了成为一名优秀DBA所需掌握的众多知识点,旨在帮助学习者深入理解MySQL数据库的运维与优化。 一、MySQL概述 MySQL是一种开源、免费的关系型数据库管理系统,广泛应用于互联网应用...
美河提供.MYSQL完全手册.pdf ,易操作
### MySQL DBA深度学习笔记知识点概述 #### 一、MySQL版本选择与下载 - **MSI与ZIP包区别**: - **MSI文件**:适用于Windows系统,提供图形化安装界面,支持自定义安装路径及组件选择等功能,适合希望进行定制化...