转 mysql 存储过程初探
https://www.cnblogs.com/qmfsun/p/4838032.html
MySQL命令执行sql文件的两种方法
https://www.cnblogs.com/mark-chan/p/5384139.html
CREATE PROCEDURE GreetWorld() SELECT CONCAT(@greeting,' World');
SET @greeting='Hello';
CALL GreetWorld();
https://blog.csdn.net/shaochenshuo/article/details/49890947
mysql中使用tee实现类似oracle spool功能
SELECT
FROM_UNIXTIME(his.clock, "%Y-%m-%d") AS DATE,
his.VALUE AS traffic,
(a.key_) AS NAME,
a.name AS db_name,
a.host AS HOST
FROM
history his,(SELECT i.key_,i.itemid,h.hostid,h.host,h.name FROM items i,HOSTS h
WHERE h.hostid=i.hostid AND i.key_ LIKE '%size%db%oradata,pfree%' ) a
WHERE his.itemid=a.itemid
#AND FROM_UNIXTIME(his.clock) >= '2019-09-16'
# AND FROM_UNIXTIME(clock) < '2019-08-11'
AND FROM_UNIXTIME(clock, '%Y-%m-%d' ) ='2019-09-16'
#GROUP BY FROM_UNIXTIME(his.clock, "%Y-%m-%d")
##https://www.cnblogs.com/mark-chan/p/5384139.html
MySQL存储过程
##参考https://www.cnblogs.com/lyhc/p/5760164.html
mysql存储过程 --游标的使用 取每行记录 (多字段)
delimiter $
create PROCEDURE get_filesystem_inf()
BEGIN
DECLARE key varchar(64); -- id
DECLARE itemid1 varchar(16); -- 监控项ID
DECLARE hostid1 varchar(32); -- hostid
DECLARE host1 varchar(64); -- 主机名
DECLARE name1 varchar(64); -- ip
-- 遍历数据结束标志
DECLARE done INT DEFAULT FALSE;
-- 游标
DECLARE cur_account CURSOR FOR SELECT i.key_,i.itemid,h.hostid,h.host,h.name FROM items i,HOSTS h
WHERE h.hostid=i.hostid AND i.key_ LIKE '%size%db%oradata,pfree%';
-- 将结束标志绑定到游标
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
END
$
delimiter $
create PROCEDURE get_filesystem_inf()
BEGIN
DECLARE key1 varchar(64); -- id
DECLARE itemid1 varchar(16); -- 监控项ID
DECLARE hostid1 varchar(32); -- hostid
DECLARE host1 varchar(64); -- 主机名
DECLARE name1 varchar(64); -- ip
-- 遍历数据结束标志
DECLARE done INT DEFAULT FALSE;
-- 游标
DECLARE cur_account CURSOR FOR SELECT i.key_,i.itemid,h.hostid,h.host,h.name FROM items i,HOSTS h
WHERE h.hostid=i.hostid AND i.key_ LIKE '%size%db%oradata,pfree%';
-- 将结束标志绑定到游标
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
-- 打开游标
OPEN cur_account;
-- 遍历
read_loop: LOOP
-- 取值 取多个字段
FETCH NEXT from cur_account INTO key,itemid1,hostid1,host1,name1;
IF done THEN
LEAVE read_loop;
END IF;
-- 你自己想做的操作
--insert into account(id,phone,password,name) value(UUID(),phone1,password1,CONCAT(name1,'的家长'));
select itemid1,host11,name1,key,FROM_UNIXTIME(his.clock, "%Y-%m-%d") AS DATE, his.VALUE AS traffic
from history his
WHERE his.itemid=itemid1
and FROM_UNIXTIME(clock, '%Y-%m-%d' ) ='2019-09-16' limit 1;
END LOOP;
CLOSE cur_account;
END $
---mysql 不支持 create or replace procedure 的写法,只支持drop and create
--
drop PROCEDURE get_filesystem_inf;
--最终模板,因为中文好像无法在MYSQL 提示符下识别,所以去掉所有中文
--错误代码: 1064 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 '' at line 3
--换行不行, 解决方法 加入 delimiter $
--将脚本 放在 sqlyog 编辑器里,使用sql 格式化下。在放在mysql 提示符下 执行
###计算所有的空间
delimiter $
CREATE PROCEDURE get_filesystem_inf ()
BEGIN
DECLARE key1 VARCHAR(64);
DECLARE itemid1 VARCHAR(16);
DECLARE hostid1 VARCHAR(32);
DECLARE host1 VARCHAR(64);
DECLARE name1 VARCHAR(64);
DECLARE done INT DEFAULT FALSE;
DECLARE cur_account CURSOR FOR
SELECT
i.key_,
i.itemid,
h.hostid,
h.host,
h.name
FROM
items i,
HOSTS h
WHERE h.hostid = i.hostid
AND i.key_ LIKE '%size%db/%/%,pfree%'
AND i.key_ NOT LIKE '%client%,pfree%';
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE ;
OPEN cur_account ;
read_loop :
LOOP
FETCH NEXT FROM cur_account INTO key1,itemid1,
hostid1,
host1,
name1 ;
IF done
THEN LEAVE read_loop ;
END IF ;
SELECT
itemid1,
host1,
name1,
key1,
FROM_UNIXTIME(his.clock, "%Y-%m-%d") AS DATE,
his.VALUE AS traffic
FROM
history his
WHERE his.itemid = itemid1
AND FROM_UNIXTIME(clock, '%Y-%m-%d') = '2019-09-16'
LIMIT 1 ;
END LOOP ;
CLOSE cur_account ;
END
##sample
###计算小于35%的空间
CREATE PROCEDURE get_filesystem_inf1 ()
BEGIN
DECLARE key1 VARCHAR(64);
DECLARE itemid1 VARCHAR(16);
DECLARE hostid1 VARCHAR(32);
DECLARE host1 VARCHAR(64);
DECLARE name1 VARCHAR(64);
DECLARE done INT DEFAULT FALSE;
DECLARE cur_account CURSOR FOR
SELECT
i.key_,
i.itemid,
h.hostid,
h.host,
h.name
FROM
items i,
HOSTS h
WHERE h.hostid = i.hostid
AND i.key_ LIKE '%size%db/%/%,pfree%'
AND i.key_ NOT LIKE '%client%,pfree%';
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE ;
OPEN cur_account ;
read_loop :
LOOP
FETCH NEXT FROM cur_account INTO key1,itemid1,
hostid1,
host1,
name1 ;
IF done
THEN LEAVE read_loop ;
END IF ;
SELECT
itemid1,
host1,
name1,
key1,
FROM_UNIXTIME(his.clock, "%Y-%m-%d") AS DATE,
his.VALUE AS traffic
FROM
history his
WHERE his.itemid = itemid1
AND FROM_UNIXTIME(clock, '%Y-%m-%d') = '2019-09-16'
and his.VALUE < 35
LIMIT 1 ;
END LOOP ;
CLOSE cur_account ;
END
tee /tmp/check1.log
call get_filesystem_inf1();
tee off
#############
https://blog.csdn.net/weixin_41986096/article/details/80464008
##查询今天
SELECT column_name(s) FROM table_name WHERE DATE_FORMAT( create_time,'%Y-%m-%d') = DATE_FORMAT(NOW(), '%Y-%m-%d');
DATE_FORMAT()函数用于以不同的格式显示日期/时间数据
DATE_FORMAT(date,format)
date参数是合法的日期。format规定日期/时间的输出格式
NOW()函数返回当前的日期和时间
##查询昨天
SELECT column_name(s) FROM table_name WHERE DATE_FORMAT( create_time,'%Y-%m-%d') = DATE_FORMAT(CURDATE()-1,'%Y-%m-%d');
SELECT column_name(s) FROM table_name WHERE DATE(create_time) =DATE(CURDATE()-1);
##输出格式:2017-04-14
SELECT CURDATE();
##输出格式:2017-04-14 13:44:11
SELECT NOW();
————————————————
版权声明:本文为CSDN博主「摘星族」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_41986096/article/details/80464008
###sample
MYSQL存储过程调试过程
作者:小虚竹
欢迎任何形式的转载,但请务必注明出处。
限于本人水平,如果文章和代码有表述不当之处,还请不吝赐教。