函数
注意与存储过程的区别,mysql内置的函数只能在sql语句中使用!
参考博客:http://www.cnblogs.com/linhaifeng/articles/7495918.html#_label2
CREATE TABLE blog ( id INT PRIMARY KEY auto_increment, NAME CHAR (32), sub_time datetime ); ? INSERT INTO blog (NAME, sub_time) VALUES (‘第1篇‘,‘2015-03-01 11:31:21‘), (‘第2篇‘,‘2015-03-11 16:31:21‘), (‘第3篇‘,‘2016-07-01 10:21:31‘), (‘第4篇‘,‘2016-07-22 09:23:21‘), (‘第5篇‘,‘2016-07-23 10:11:11‘), (‘第6篇‘,‘2016-07-25 11:21:31‘), (‘第7篇‘,‘2017-03-01 15:33:21‘), (‘第8篇‘,‘2017-03-01 17:32:21‘), (‘第9篇‘,‘2017-03-01 18:31:21‘); ? select date_format(sub_time,‘%Y-%m‘),count(id) from blog group by date_format(sub_time,‘%Y-%m‘); # 查询结果:
+-------------------------------+-----------+
| date_format(sub_time,"%Y-%m") | count(id) |
+-------------------------------+-----------+
| 2015-03 | 2 |
| 2016-07 | 4 |
| 2017-03 | 3 |
+-------------------------------+-----------+
流程控制
# if条件语句 delimiter // CREATE PROCEDURE proc_if () BEGIN declare i int default 0; if i = 1 THEN SELECT 1; ELSEIF i = 2 THEN SELECT 2; ELSE SELECT 7; END IF; ? END // delimiter ; # while循环 delimiter // CREATE PROCEDURE proc_while () BEGIN ? DECLARE num INT ; SET num = 0 ; WHILE num < 10 DO SELECT num ; SET num = num + 1 ; END WHILE ; ? END // delimiter ;
原文地址:https://www.cnblogs.com/dongxixi/p/10885297.html
时间: 2024-09-30 06:26:36