操作環境:有表game_list,字段:uid,score1,score2,seat_id,last_update;
傳入參數為i_player_detail ,傳入的值為多個用戶的id、之前分數、之後分數、座位號,每個用戶的數據用分號(;)隔開;
操作目的:將各個用戶對應的屬性插入到目標表對應的字段中,last_update為數據更新日期;
傳入參數i_player_detail ,裡面存放多個用戶的信息,每個用戶的一組數據用分號隔開,每個用戶的信息多個,比如
“用戶id,score,desk,seat;
用戶id,score,desk,seat;……”
-- 使用存儲過程 delimiter $$ use `log_pdk`$$ drop procedure if exists `game_c`$$ create procedure `game_c` (in i_player_detail varchar(500)) SQL SECURITY INVOKER BEGIN DROP TABLE IF EXISTS `temp_list`; --創建臨時表,將截取的數據先插入到臨時表 CREATE TEMPORARY TABLE `temp_list`( `uid` INT(10) UNSIGNED NOT NULL, `score1` INT(10) UNSIGNED NOT NULL, `score2` INT(10) UNSIGNED NOT NULL, `seat_id` TINYINT(3) UNSIGNED NOT NULL ); -- declare str varchar(500);-- 用來拼接sql動態語句 declare m_detail varchar(500); declare m_num tinyint; -- 當傳入的用戶信息字符串中含有分號';',進行截取 set m_num = position(';' in str) -- 不存在分號的時候,返回0 while m_num >= 1 do begin set @str = 'insert into temp_list values (' + substring(m_detail,1,m_num-1)+')' -- 截取第一個用戶的信息(第一個分號前面的字符),插入到臨時表 prepare statement1 from @str; execute statement1; deallocate prepare statement1; set m_detail = substring(m_detail,m_num+1); -- 定義除去第一個用戶和分號那部分的字符串 set set m_num = position(';' in str); end while; -- 從臨時表抽出所有字段,添加時間字段,插入到表game_list INSERT INTO `game_list`(`uid`,`score1`,`score2`,`seat_id`, `last_update`) SELECT `uid`, `score1`, `score2`, `seat_id`, current_date() FROM `temp_list`; end$$ delimiter ;
insert into t2 (id,old,up,new,qq,time) select id ,gold,1 ,gold+1,qq,sysdate() from t1 where qq in (qq1,qq2,qq3);
SELECT *
FROM `order`
INNER JOIN `userinfo` ON `userinfo`.`userId` = `order`.`uid`