当前位置: > 数据库 > MySQL >

7个比较适用mysql函数举例

时间:2014-07-06 17:11来源:linux.it.net.cn 作者:IT网

mysql的函数有很多,下面举几个适用,但是容易被人忽视的函数

 

一,准备测试表和数据

1,测试表

查看复制打印?
  1. CREATE TABLE `comment` (  
  2.  `c_id` int(11) NOT NULL auto_increment COMMENT '评论ID',  
  3.  `u_id` int(11) NOT NULL COMMENT '用户ID',  
  4.  `name` varchar(50) NOT NULL default '' COMMENT '用户名称',  
  5.  `content` varchar(1000) NOT NULL default '' COMMENT '评论内容',  
  6.  `datetime` timestamp NOT NULL default CURRENT_TIMESTAMP,  
  7.  `num1` int(11) default NULL,  
  8.  `num2` int(11) default NULL,  
  9.  PRIMARY KEY  (`c_id`)  
  10. ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;  

2,测试表数据

c_id u_id name content datetime num1 num2
1 1 test 2222222211 2010-11-10 15:16:00 21 12
2 1 test2 tank 2010-11-10 15:01:00 133 219
3 2 tank zhangy 2010-11-10 15:11:00 67 16
4 3 test4 test 2010-11-15 16:01:26 34 NULL

二,mysql常用函数,以及实例

1,GREATEST(求最大值)和LEAST(求最小值)

查看复制打印?
  1. mysql> SELECT c_id, GREATEST( num1, num2 ) AS max, num1, num2 from comment where  
  2.  num1 != "" and num2 != "";  
  3. +------+------+------+------+  
  4. | c_id | max  | num1 | num2 |  
  5. +------+------+------+------+  
  6. |    1 |   21 |   21 |   12 |  
  7. |    2 |  219 |  133 |  219 |  
  8. |    3 |   67 |   67 |   16 |  
  9. +------+------+------+------+  
  10. 3 rows in set (0.00 sec)  
  11.   
  12. mysql> SELECT c_id, LEAST( num1, num2 ) AS max, num1, num2 from comment where nu  
  13. m1 != "" and num2 != "";  
  14. +------+------+------+------+  
  15. | c_id | max  | num1 | num2 |  
  16. +------+------+------+------+  
  17. |    1 |   12 |   21 |   12 |  
  18. |    2 |  133 |  133 |  219 |  
  19. |    3 |   16 |   67 |   16 |  
  20. +------+------+------+------+  
  21. 3 rows in set (0.00 sec)  

2,CONCAT_WS函数

查看复制打印?
  1. mysql> SELECT CONCAT_WS( ',', name, content, datetime ) FROM `comment`;  
  2. +-------------------------------------------+  
  3. | CONCAT_WS( ',', name, content, datetime ) |  
  4. +-------------------------------------------+  
  5. | test,2222222211,2010-11-10 15:16:00       |  
  6. | test2,tank,2010-11-10 15:01:00            |  
  7. | tank,zhangy,2010-11-10 15:11:00           |  
  8. | test4,test,2010-11-15 16:01:26            |  
  9. +-------------------------------------------+  
  10. 4 rows in set (0.00 sec)  

3,INTERVAL函数

查看复制打印?
  1. mysql> select * from comment where   datetime  <= (SELECT now( ) - INTERVAL 10 h  
  2. our AS time_start );  //10个小时前评论的数据  
  3. +------+------+-------+------------+---------------------+------+------+  
  4. | c_id | u_id | name  | content    | datetime            | num1 | num2 |  
  5. +------+------+-------+------------+---------------------+------+------+  
  6. |    1 |    1 | test  | 2222222211 | 2010-11-10 15:16:00 |   21 |   12 |  
  7. |    2 |    1 | test2 | tank       | 2010-11-10 15:01:00 |  133 |  219 |  
  8. |    3 |    2 | tank  | zhangy     | 2010-11-10 15:11:00 |   67 |   16 |  
  9. +------+------+-------+------------+---------------------+------+------+  
  10. 3 rows in set (0.00 sec)  

4,last_insert_id函数

查看复制打印?
  1. mysql> insert into comment(u_id,name,content,datetime,num1,num2)values(2,'test5'  
  2. ,'good',now(),3,4);  
  3. Query OK, 1 row affected (0.00 sec)  
  4.   
  5. mysql> select last_insert_id();   //取得最后一次插入的ID  
  6. +------------------+  
  7. | last_insert_id() |  
  8. +------------------+  
  9. |                5 |  
  10. +------------------+  
  11. 1 row in set (0.00 sec)  

5,REGEXP函数

查看复制打印?
  1. mysql> SELECT * FROM `comment` WHERE content REGEXP '[0-9]+';  
  2. +------+------+------+------------+---------------------+------+------+  
  3. | c_id | u_id | name | content    | datetime            | num1 | num2 |  
  4. +------+------+------+------------+---------------------+------+------+  
  5. |    1 |    1 | test | 2222222211 | 2010-11-10 15:16:00 |   21 |   12 |  
  6. +------+------+------+------------+---------------------+------+------+  
  7. 1 row in set (0.00 sec)  

6,rand函数

查看复制打印?
  1. mysql> select cast(rand()*100 as unsigned) as rand_num;  
  2. +----------+  
  3. | rand_num |  
  4. +----------+  
  5. |       57 |  
  6. +----------+  
  7. 1 row in set (0.00 sec)  

7,常用日期函数day,month,hour,time,now等等

查看复制打印?
  1. mysql> select * from comment where day(datetime) = '15' limit 1;  
  2. +------+------+-------+---------+---------------------+------+------+  
  3. | c_id | u_id | name  | content | datetime            | num1 | num2 |  
  4. +------+------+-------+---------+---------------------+------+------+  
  5. |    4 |    3 | test4 | test    | 2010-11-15 16:01:26 |   34 | NULL |  
  6. +------+------+-------+---------+---------------------+------+------+  
  7. 1 row in set (0.00 sec)  
  8.   
  9. mysql> select * from comment where month(datetime) = '11' limit 1;  
  10. +------+------+------+------------+---------------------+------+------+  
  11. | c_id | u_id | name | content    | datetime            | num1 | num2 |  
  12. +------+------+------+------------+---------------------+------+------+  
  13. |    1 |    1 | test | 2222222211 | 2010-11-10 15:16:00 |   21 |   12 |  
  14. +------+------+------+------------+---------------------+------+------+  
  15. 1 row in set (0.00 sec)  
  16.   
  17. mysql> select week(now());    //显示当前周数  
  18. +-------------+  
  19. | week(now()) |  
  20. +-------------+  
  21. |          46 |  
  22. +-------------+  
  23. 1 row in set (0.00 sec)  
  24.   
  25. mysql> select hour(now());   //显示当前的小时数  
  26. +-------------+  
  27. | hour(now()) |  
  28. +-------------+  
  29. |          17 |  
  30. +-------------+  
  31. 1 row in set (0.00 sec)  
  32.   
  33. mysql> select c_id,date_format(datetime,'%Y%m%d%H%i%s'as new_date from comment  
  34. ;     //格式化日期  
  35. +------+----------------+  
  36. | c_id | new_date       |  
  37. +------+----------------+  
  38. |    1 | 20101110151600 |  
  39. |    2 | 20101110150100 |  
  40. |    3 | 20101110151100 |  
  41. |    4 | 20101115160126 |  
  42. |    5 | 20101115173546 |  
  43. +------+----------------+  
  44. 5 rows in set (0.00 sec)  

了解这些函数其他,可以使我们的程序更加的有效一点,代码更加的简洁一点。由于个人习惯问题,

select * from comment where day(datetime) = '15'

我习惯了下面的写法,上面这个方法更加的明了

select * from comment where substring(datetime,9,2) = '15';

(责任编辑:IT)
------分隔线----------------------------
栏目列表
推荐内容