1. 程式人生 > >mysql 存儲過程的應用

mysql 存儲過程的應用

double sel .net mes comment 原創文章 where update 我們

版權聲明:本文為博主原創文章,未經博主同意不得轉載。 https://blog.csdn.net/huahuagongzi9999/article/details/26266101


?MySQL存儲過程的創建

?

(1).?格式

MySQL存儲過程創建的格式:CREATE PROCEDURE?過程名?([過程參數[,...]])
[
特性?...]

?過程體

這裏先舉個樣例:
???

  1. mysql>?DELIMITER?// ?
  2. mysql>?CREATE?PROCEDURE?proc1(OUT?s?int) ?
  3. ????->?BEGIN?
  4. ????->?SELECT?COUNT(*)?INTO?s?FROM?user; ?
  5. ????->?END?
  6. ????->?// ?
  7. mysql>?DELIMITER?;?

註:

1)這裏須要註意的是DELIMITER //DELIMITER ;兩句。DELIMITER是切割符的意思,由於MySQL默認以";"為分隔符,假設我們沒有聲明切割符,那麽編譯器會把存儲過程當成

SQL語句進行處理,則存儲過程的編譯過程會報錯,所以要事先用DELIMITERkeyword申明當前段分隔符。這樣MySQL才會將";"當做存儲過程中的代碼。不會運行這些代碼,用完了之後要把分隔符還原。

2)存儲過程依據須要可能會有輸入、輸出、輸入輸出參數,這裏有一個輸出參數s,類型是int型。假設有多個參數用","切割開。

3)過程體的開始與結束使用BEGINEND進行標識。

這樣,我們的一個MySQL存儲過程就完畢了,是不是非常easy呢?看不懂也沒關系,接下來,我們具體的解說。



以下的樣例主要用到了

. if-then -else語句

. ?FOUND_ROWS()?

語句


#記錄每天的步行、睡眠、體重、消耗卡路裏等信息
#userRecordDetail 表中,假設存在當天數據,則改動。否則新增
#userRecord 表中,假設存在,則累加,否則新增
#類型:1步行2睡眠3卡路裏消耗4體重
#CALL userRecord_create(1001,45,100,1000,1000,500,500,2,1);  
DROP PROCEDURE IF EXISTS pro_userRecord_stepNum; 
DELIMITER //  
CREATE PROCEDURE pro_userRecord_stepNum(IN p_userId INT,IN p_stepNum INT)
BEGIN
    DECLARE RCount INT;  
    -- 查看用戶是否有具體記錄
    SELECT id FROM userRecordDetail WHERE userId = p_userId AND DATE(createTime) = CURDATE() LIMIT 1;
    SELECT FOUND_ROWS() INTO RCount;
    IF (RCount=0) THEN
        --  查看userRecord是否實用戶總記錄信息,不存在,則加入,否則改動
        SELECT id  FROM userRecord WHERE userId = p_userId LIMIT 1;    
        SELECT FOUND_ROWS() INTO RCount; 
        IF(RCount = 0 )THEN 
           INSERT  INTO `userRecord`(`userId`,`totalStep`,`updateTime`,`createTime`) 
           VALUES (p_userId,p_stepNum,NOW(),NOW()); 
        ELSE
           UPDATE userRecord SET totalStep = totalStep+p_stepNum WHERE userId = p_userId;
        END IF;  -- 結束
        -- 插入一條用戶記錄具體信息
        INSERT  INTO `userRecordDetail`(`weigh`,`calorie`,`stepNum`,`userId`,
        `sleepTimes`,`lightSleepTimes`,`heavySleepTimes`,
        `wakeupNum`,`updateTime`,`createTime`)
        VALUES (0,0,p_stepNum, p_userId,0,0,0,0,NOW(),NOW());       
    ELSE
       --  查看是否實用戶總記錄信息,不存在。則加入,否則改動
        SELECT id  FROM userRecord WHERE userId = p_userId LIMIT 1;     
        SELECT FOUND_ROWS() INTO RCount; 
        IF(RCount = 0 )THEN 
           INSERT  INTO `userRecord`(`userId`,`totalStep`,`updateTime`,`createTime`) 
           VALUES (p_userId,p_stepNum,NOW(),NOW()); 
        ELSE
           UPDATE userRecord SET totalStep = totalStep + p_stepNum WHERE userId = p_userId;
        END IF;
         -- 改動userRecordDetail
        UPDATE userRecordDetail SET stepNum = stepNum + p_stepNum WHERE userId = p_userId;
    END IF;
END;//  
DELIMITER ; 
SHOW WARNINGS;   
SHOW CREATE PROCEDURE pro_userRecord_stepNum;
CALL pro_userRecord_stepNum(1009,111);   


假設須要精確到小時,則存儲過程語句例如以下:

#記錄每天的步行、睡眠、體重、消耗卡路裏等信息
#userRecordDetail 表中。假設存在當天數據。則改動,否則新增
#userRecord 表中,假設存在,則累加,否則新增
#類型:1步行2睡眠3卡路裏消耗4體重
#CALL userRecord_create(1001,45,100,1000,1000,500,500,2,1);  
DROP PROCEDURE IF EXISTS pro_userRecord_sleep; 
DELIMITER //  
CREATE PROCEDURE pro_userRecord_sleep(IN p_userId INT,
IN p_sleepTimes INT ,
IN p_lightSleepTimes INT ,
IN p_heavySleepTimes INT ,
IN p_wakeupNum INT) 
BEGIN
    DECLARE RCount INT;
    DECLARE resultId INT;
    -- 查看用戶是否有具體記錄
    SELECT id INTO resultId  FROM userRecordDetail WHERE userId = p_userId AND DATE_FORMAT(createTime,‘%Y%m%d%H‘) = DATE_FORMAT(NOW(),‘%Y%m%d%H‘) LIMIT 1; 
    SELECT FOUND_ROWS() INTO RCount;
    IF (RCount=0) THEN
        --  查看userRecord是否實用戶總記錄信息。不存在,則加入,否則改動
        SELECT id  FROM userRecord WHERE userId = p_userId LIMIT 1;    
        SELECT FOUND_ROWS() INTO RCount; 
        IF(RCount = 0 )THEN 
           INSERT  INTO `userRecord`(`userId`,`totalStep`,`updateTime`,`createTime`) 
           VALUES (p_userId,p_stepNum,NOW(),NOW()); 
        ELSE
           UPDATE userRecord SET totalStep = totalStep+p_stepNum WHERE userId = p_userId;
        END IF;  -- 結束
        -- 插入一條用戶記錄具體信息
        INSERT  INTO `userRecordDetail`(`weigh`,`calorie`,`stepNum`,`userId`,
        `sleepTimes`,`lightSleepTimes`,`heavySleepTimes`,
        `wakeupNum`,`updateTime`,`createTime`)
        VALUES (0,0,0, p_userId,p_sleepTimes,p_lightSleepTimes,p_heavySleepTimes,p_wakeupNum,NOW(),NOW());       
    ELSE
        -- 改動userRecordDetail
         UPDATE userRecordDetail SET 
         sleepTimes = sleepTimes + p_sleepTimes,
         lightSleepTimes = lightSleepTimes + p_lightSleepTimes,
         heavySleepTimes = heavySleepTimes + p_heavySleepTimes,
         wakeupNum = wakeupNum + p_wakeupNum
         WHERE id = resultId;
    END IF;
END;//  
DELIMITER ; 
SHOW WARNINGS;   
SHOW CREATE PROCEDURE pro_userRecord_sleep;
CALL pro_userRecord_sleep(1009,600,100,500,2); 






. 創建表的語句例如以下:



DROP TABLE IF EXISTS `userRecord`;

CREATE TABLE `userRecord` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `userId` int(11) NOT NULL COMMENT ‘fk‘,
  `totalStep` int(11) DEFAULT ‘0‘ COMMENT ‘總步數‘,
  `updateTime` datetime DEFAULT NULL,
  `createTime` datetime NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=8 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT=‘用戶記錄總表‘;

/*Data for the table `userRecord` */

LOCK TABLES `userRecord` WRITE;

insert  into `userRecord`(`id`,`userId`,`totalStep`,`updateTime`,`createTime`) values (1,1001,88000,‘2014-05-16 14:16:50‘,‘2014-05-13 14:16:52‘),(2,1002,35000,‘2014-05-16 14:26:22‘,‘2014-05-12 14:26:24‘),(3,1003,95000,‘2014-05-16 14:28:00‘,‘2014-05-12 14:28:06‘),(4,1007,150000,‘2014-05-16 14:30:31‘,‘2014-04-28 14:30:33‘),(5,1009,288,‘2014-05-19 16:24:26‘,‘2014-05-19 16:24:26‘),(6,1010,33,‘2014-05-19 17:01:50‘,‘2014-05-19 17:01:50‘),(7,1011,33,‘2014-05-19 17:03:31‘,‘2014-05-19 17:03:31‘);

UNLOCK TABLES;

/*Table structure for table `userRecordDetail` */

DROP TABLE IF EXISTS `userRecordDetail`;

CREATE TABLE `userRecordDetail` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `weigh` double DEFAULT ‘0‘ COMMENT ‘今日體重 kg‘,
  `calorie` int(11) DEFAULT ‘0‘ COMMENT ‘今日消耗卡路裏‘,
  `stepNum` int(11) DEFAULT ‘0‘ COMMENT ‘今日步數‘,
  `userId` int(11) NOT NULL COMMENT ‘fk‘,
  `sleepTimes` int(11) DEFAULT ‘0‘ COMMENT ‘今日睡眠時間 單位:分鐘‘,
  `lightSleepTimes` int(11) DEFAULT ‘0‘ COMMENT ‘今日輕度睡眠時間 單位:分鐘‘,
  `heavySleepTimes` int(11) DEFAULT ‘0‘ COMMENT ‘今日重度睡眠時間 單位:分鐘‘,
  `wakeupNum` int(11) DEFAULT ‘0‘ COMMENT ‘今日喚醒次數‘,
  `updateTime` datetime DEFAULT NULL,
  `createTime` datetime NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=26 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT=‘用戶記錄具體信息表‘;

/*Data for the table `userRecordDetail` */

LOCK TABLES `userRecordDetail` WRITE;

insert  into `userRecordDetail`(`id`,`weigh`,`calorie`,`stepNum`,`userId`,`sleepTimes`,`lightSleepTimes`,`heavySleepTimes`,`wakeupNum`,`updateTime`,`createTime`) values (1,0,0,10000,1001,0,0,0,0,NULL,‘2014-05-16 14:17:53‘),(2,0,0,10000,1001,0,0,0,0,NULL,‘2014-05-15 14:22:58‘),(3,0,0,15000,1001,0,0,0,0,NULL,‘2014-05-14 14:23:56‘),(4,0,0,13000,1001,0,0,0,0,NULL,‘2014-05-13 14:24:10‘),(5,0,0,20000,1001,0,0,0,0,NULL,‘2014-05-12 14:24:32‘),(6,0,0,8000,1001,0,0,0,0,NULL,‘2014-05-11 14:24:51‘),(7,0,0,12000,1001,0,0,0,0,NULL,‘2014-05-09 14:25:02‘),(8,0,0,10000,1002,0,0,0,0,NULL,‘2014-05-16 14:26:50‘),(9,0,0,5000,1002,0,0,0,0,NULL,‘2014-05-15 14:26:58‘),(10,0,0,20000,1002,0,0,0,0,NULL,‘2014-05-14 14:27:14‘),(11,0,0,20000,1003,0,0,0,0,NULL,‘2014-05-16 14:28:46‘),(12,0,0,30000,1003,0,0,0,0,NULL,‘2014-05-15 14:28:54‘),(13,0,0,25000,1003,0,0,0,0,NULL,‘2014-05-13 14:29:01‘),(14,0,0,15000,1003,0,0,0,0,NULL,‘2014-05-12 14:29:07‘),(15,0,0,5000,1003,0,0,0,0,NULL,‘2014-05-08 14:29:39‘),(16,0,0,20000,1007,0,0,0,0,NULL,‘2014-05-16 14:30:45‘),(17,0,0,30000,1007,0,0,0,0,NULL,‘2014-05-15 14:30:54‘),(18,0,0,25000,1007,0,0,0,0,NULL,‘2014-05-14 14:31:02‘),(19,0,0,15000,1007,0,0,0,0,NULL,‘2014-05-13 14:31:10‘),(20,0,0,35000,1007,0,0,0,0,NULL,‘2014-05-12 14:31:18‘),(21,0,0,25000,1007,0,0,0,0,NULL,‘2014-05-11 14:31:26‘),(22,0,0,20000,1007,0,0,0,0,NULL,‘2014-04-30 14:32:02‘),(23,45,111,288,1009,600,100,500,2,‘2014-05-19 16:24:26‘,‘2014-05-19 16:24:26‘),(24,0,66,33,1010,0,0,0,0,‘2014-05-19 17:01:50‘,‘2014-05-19 17:01:50‘),(25,45,33,33,1011,600,100,500,0,‘2014-05-19 17:03:31‘,‘2014-05-19 17:03:31‘);

UNLOCK TABLES;





以下的樣例主要用到了

. if-then -else語句

mysql 存儲過程的應用