1. 程式人生 > >mysql定時執行查詢插入資料操作

mysql定時執行查詢插入資料操作

1.在資料庫函式中建立一個自定義函式

2.CREATE DEFINER=`root`@`localhost` PROCEDURE `addAlarmToMesPool`()————建立一個函式

BEGIN

(中間部分就是SQL執行語句)

END

3.自定義函式中可以執行多條SQL語句 以';'區分

4.程式碼演示

CREATE DEFINER=`root`@`localhost` PROCEDURE `addAlarmToMesPool`()
BEGIN
INSERT INTO t_log_message_pool ( admin_id, alarm_id, print_point_name, policy_name, alarm_message, alarm_time, send_type, phone, email, state, createdate, updatetime,post_mail )
SELECT 
	sub.admin_id,
	lgam.id alarm_id,				
	ptst.print_point_name,  
	sub.NAME policy_name,
	lgam.alarm_message, 		
	lgam.alarm_time,	
	sub.send_type,
	sub.phone,
	sub.email,
	0 state,
	NOW( ) createdate,
	NOW( ) updatetime,
	sub.post_email	
FROM 
	t_log_alarm lgam
		INNER JOIN t_s_printstation ptst ON lgam.station_id = ptst.id
		INNER JOIN (
			SELECT 
				sbsb.admin_id,						
				admin.mobile_phone phone,	
				admin.mail email,					
				sbsb.member_type,					
				sbsb.member_id,						
				sbsb.send_type,						
				sbsb.updatedate,					
				tspt.NAME,								
				ptam.type,								
				ptam.alarm_code,					
				sbsb.email post_email
			FROM
				t_s_subscribe sbsb
				INNER JOIN t_s_policytype tspt ON sbsb.policytype_id = tspt.id
				INNER JOIN t_s_policytype_alarm ptam ON tspt.id = ptam.policytype_id
				INNER JOIN t_s_admin admin ON sbsb.admin_id = admin.id 
			WHERE
				sbsb.state = 1
				AND ptam.state = 1
				AND sbsb.member_type = 1
				AND tspt.type = 1
				AND admin.state = 1
		) sub ON  
				lgam.alarm_code = sub.alarm_code
				AND sub.type = 1  
				AND lgam.alarm_time >= sub.updatedate 
WHERE
	lgam.state = 1
	AND ptst.print_state <>- 1 	
GROUP BY
	lgam.id;	
UPDATE t_s_subscribe  SET updatedate = NOW( ) ;
END

5.建立定時執行時間

在資料庫時間中建立時間  

5.1執行之定義函式  

5.2設定時間週期

5.3開啟設定