DECLARE done tinyint default 0; DECLARE uid int(11); DECLARE cs CURSOR FOR SELECT userId FROM tbuser_red_envelope_award_log WHERE fromActiveId="1454136362"; DECLARE continue handler for sqlstate '02000' set done=1; open cs; while done<>1 do fetch cs into uid; SELECT giftNum into @giftNum FROM tbuser_red_envelope_award_log WHERE userId=uid AND gifttemplateId=101; IF EXISTS(SELECT id FROM tbuser_bag WHERE awardNum>=@giftNum AND userId=uid AND gifttemplateId=101)THEN UPDATE tbuser_bag SET awardNum=awardNum-@giftNum WHERE awardNum>=@giftNum AND userId=uid AND gifttemplateId=101; END IF; IF EXISTS(SELECT id FROM tbuser_bag_exchange WHERE userId=uid AND excNum>=@giftNum-5 AND addTime>"2016-01-30 18:00:00")THEN UPDATE tbuser_bag_exchange SET excNum=excNum-@giftNum,excTotalScore=excTotalScore-@giftNum WHERE userId=uid AND excNum>=@giftNum-5 AND addTime>"2016-01-30 18:00:00"; END IF; DELETE FROM tbuser_bag_exchange WHERE excNum<=0 AND addTime>"2016-01-30 18:00:00"; end while; close cs;
BEGIN
		DECLARE $gameId int;			
		DECLARE STOP INT DEFAULT 0;
		DECLARE game_id CURSOR FOR SELECT id FROM tbgames_box_activity;
		DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET STOP=1;
		OPEN  game_id;
		FETCH game_id INTO $gameId;
		WHILE STOP <> 1 DO
				SET @today = CONCAT(date(NOW())," 00:00:01");
				IF EXISTS(SELECT id  FROM tbgames_box_activity WHERE id = $gameId AND addTime<@today AND isOpen=1 LIMIT 1) THEN
					SELECT awardPool,smallAwardBoxNum,awardAllocType,awardPoolType,awardPoolRemainder INTO @awardPool, @smallAwardBoxNum,@awardAllocType,@awardPoolType,@awardPoolRemainder 
							FROM tbgames_box_activity WHERE id = $gameId AND addTime<@today AND isOpen=1 LIMIT 1;
					IF @awardPoolType=0 THEN
						IF @awardAllocType=1 THEN 
							SET @num=FLOOR(1 + (RAND() * 3));
							UPDATE tbgames_box_activity SET awardPoolRemainder=@awardPool+@awardPoolRemainder,realBoxNum=@smallAwardBoxNum,addTime=NOW(),bigAwardBoxNum=@num WHERE id = $gameId AND addTime<@today;
							UPDATE tbgames_box_activity SET awardPoolRemainder=@awardPool+@awardPoolRemainder,realBoxNum=@smallAwardBoxNum,addTime=NOW() WHERE id = $gameId AND addTime<@today;
						END IF;
						IF @awardAllocType=1 THEN 
							SET @num=FLOOR(1 + (RAND() * 3));
							UPDATE tbgames_box_activity SET awardPoolRemainder=@awardPool,realBoxNum=@smallAwardBoxNum,addTime=NOW(),bigAwardBoxNum=@num WHERE id = $gameId AND addTime<@today;
							UPDATE tbgames_box_activity SET awardPoolRemainder=@awardPool,realBoxNum=@smallAwardBoxNum,addTime=NOW() WHERE id = $gameId AND addTime<@today;
						END IF;
					END IF;
				END IF;	
		FETCH game_id INTO $gameId;  
		END WHILE; 
		CLOSE game_id;
                    从一表中查询出一列数据,根据这一列数据来从另外一表中进行 判断修改等操作BEGIN	DECLARE done tinyint default 0;	DECLARE uid int(11);	DECLARE cs CURSOR FOR SELECT userId FROM tbuser_red_envelope_award_log WHERE fromActiveId="14541
				
如果你想在 MySQL 中使用一个查询的结果作为循环的条件,你可以使用一个游标。游标是一种数据库对象,它允许你在结果集中游动,并在需要时逐行处理数据。 首先,你需要声明一个游标,然后打开它。然后,你可以使用 FETCH 语句从游标中获取行。你可以在循环中使用 FETCH 语句,并使用游标中的数据作为条件执行查询。最后,记得关闭和释放游标。 下面是一个例子: DECLARE cur CURSOR F...
3,代码: SELECT student.*,score.s_score from student ,score WHERE student.s_id=score.s_id and score.c_id="01" AND score.s_sco...
# update select 语句(注意:必须使用inner join) # 语法 update a inner join (select yy from b) c on a.id =c.id set a.xx = c.yy #SELECT knowledge_id,COUNT(0) nums FROM pro_base_timu_knowledge G...
今天线上碰到一个问题,现场进行了两个服务器的迁移,数据库也进行了合并,导致表adm_dev的REGIST_PROXY_PORT字段出现重复数据(暂时 不考虑表结构设计问题),现在需要把此重复的数据修改 ** 一 使用mysql自带的语法实现** UPDATE mytable SET myfield = CASE id WHEN 1 THEN ‘value’ WHEN 2 THEN ‘value’ WHEN 3 THEN ‘value’ WHERE id IN (1,2,3) 由于case wh 如前一篇文章所说,如果想要验证大数据下的场景,必须要批量造数据。批量造数据既可以通过Java实现,也可以通过MySQL本身的功能实现,本章主要学习如何使用MySQL循环语句造大量数据。数据表准备请参照前一篇文章,这里就不重复了,直接上SQL。 https://blog.csdn.net/m0_37824308/article/details/121974262 二、使用while语句写循环 SELECT * from t_t_user; DELETE from t_t_user; BEGIN DECLARE i INT DEFAULT 1; -- 声明并初始化循环变量 DECLARE max_value INT DEFAULT 10; -- 声明并初始化循环终止条件 -- 开始循环 WHILE i <= max_value DO SELECT i; -- 执行查询语句 SET i = i + 1; -- 增加循环变量 END WHILE; END// DELIMITER ; 在上面的例子中,我们声明了一个名为 `i` 的循环变量,并将其初始化为 `1`。我们还声明了一个名为 `max_value` 的变量,并将其初始化为 `10`。然后我们使用 `WHILE` 循环,当 `i` 小于等于 `max_value` 时执行循环体内的语句。在循环体内,我们执行一个查询语句,并将 `i` 增加 `1`。这样,在循环结束之前,我们就会执行十次查询。 注意:上面的示例使用了存储过程,因此需要使用 `DELIMITER` 语句更改语句结束符。