mysql存儲過程 驗證登錄,添加登錄記錄主鍵id
阿新 • • 發佈:2019-03-14
end out 效果 賬號 reat family font microsoft _id
建表語句
create table `t_user` ( `id` bigint (50), `user_name` varchar (60), `pass_word` varchar (60) ); insert into `t_user` (`id`, `user_name`, `pass_word`) values(‘1‘,‘張三‘,‘abcdef‘); insert into `t_user` (`id`, `user_name`, `pass_word`) values(‘2‘,‘李四‘,‘654321‘);
create table`t_login` ( `id` bigint (20), `uid` bigint (20), `login_time` datetime ); insert into `t_login` (`id`, `uid`, `login_time`) values(‘1‘,‘1‘,‘2019-03-15 15:03:41‘); insert into `t_login` (`id`, `uid`, `login_time`) values(‘2‘,‘1‘,‘2019-03-11 15:03:44‘); insert into `t_login` (`id`, `uid`, `login_time`) values(‘3‘,‘2‘,‘2019-03-12 15:03:47‘); insert into `t_login` (`id`, `uid`, `login_time`) values(‘4‘,‘2‘,‘2019-03-10 15:03:48‘); insert into `t_login` (`id`, `uid`, `login_time`) values(‘5‘,‘2‘,‘2019-03-16 15:03:50‘); insert into `t_login` (`id`, `uid`, `login_time`) values(‘6‘,‘1‘,‘2019-03-13 17:38:17‘); insert into`t_login` (`id`, `uid`, `login_time`) values(‘7‘,‘1‘,‘2019-03-13 17:47:38‘); insert into `t_login` (`id`, `uid`, `login_time`) values(‘8‘,‘1‘,‘2019-03-13 17:48:58‘); insert into `t_login` (`id`, `uid`, `login_time`) values(‘9‘,‘1‘,‘2019-03-13 17:48:59‘); insert into `t_login` (`id`, `uid`, `login_time`) values(‘10‘,‘1‘,‘2019-03-13 17:49:06‘); insert into `t_login` (`id`, `uid`, `login_time`) values(‘11‘,‘1‘,‘2019-03-13 17:53:32‘); insert into `t_login` (`id`, `uid`, `login_time`) values(‘12‘,‘1‘,‘2019-03-13 17:56:52‘); insert into `t_login` (`id`, `uid`, `login_time`) values(‘13‘,‘1‘,‘2019-03-13 17:57:08‘); insert into `t_login` (`id`, `uid`, `login_time`) values(‘14‘,‘1‘,‘2019-03-14 12:55:02‘); insert into `t_login` (`id`, `uid`, `login_time`) values(‘15‘,‘1‘,‘2019-03-14 12:56:38‘);
存儲過程
DELIMITER $$ USE `demo`$$ DROP PROCEDURE IF EXISTS `student_03`$$ CREATE DEFINER=`root`@`%` PROCEDURE `student_03`(IN sn VARCHAR(20),IN sc VARCHAR(20),OUT st INT) BEGIN DECLARE state INT DEFAULT 0; /*驗證賬號密碼是否存在記錄>0 into賦值給 state 需要提前定義state參數*/ SELECT COUNT(1) INTO state FROM student WHERE sname = sn AND SCLASS =sc; /*if else 判斷*/ IF state > 0 THEN /*存在記錄登錄成功添加登錄記錄 userid 和 時間*/ INSERT INTO t_login (uid,login_time) VALUES(1,NOW()); /*查詢記錄id賦值st 返回 ,SET st = (SELECT LAST_INSERT_ID())效果一樣使用set賦值*/ SELECT LAST_INSERT_ID() INTO st; ELSE /*<0 返回0*/ SET st=0; END IF; END$$ DELIMITER ;
執行結果:
mysql存儲過程 驗證登錄,添加登錄記錄主鍵id