在MySQL中使用游标进行此更新查询有什么问题?

时间:2018-10-13 12:44:40

标签: mysql

我有一个表,该表的列为order_id,类型为int,它们是1到30之间的30个数字,并且是唯一的。

我想通过MySQL中的查询随机更新此列,但它们仍然必须唯一并且在 1到30范围内 。 我试过了,但它的问题是服务器中的“ phpmyadmin”,没有显示问题! 有人知道是什么问题吗?

declare Qid2 int;
declare counter int;
DECLARE done INT DEFAULT FALSE;

declare c cursor
set c = cursor for select order_id from Table_1
open c;
fetch next from c into Qid2;

DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
while !done DO


  set Qid2 = (select RAND()*30);
  set counter = ( select count(*) from Table_1 where order_id = Qid2);
  while counter!=0 do
    Begin
      set Qid2 = (select RAND()*30);
       set counter =( select count(*) from Table_1 where order_id = Qid2);
    End while;
  update Table_1
  set order_id = Qid2  where current of c;
  fetch next from c ;


End while;
close c;

2 个答案:

答案 0 :(得分:0)

在逻辑上不做任何改动,我可以识别出6个错误(如果尚未设置分隔符,则可能为7个。我已纠正了明显的错误。

drop procedure if exists p;
delimiter $$
create procedure p()
begin 
declare Qid2 int;
declare counter int;
declare order_id int; #error 3 not declared
DECLARE done INT DEFAULT FALSE;

declare c cursor for select order_id from Table_1; #error 1 incorrect syntax
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE; #error 2 wrong position in code

open c;
fetch next from c into Qid2;

while !done DO


  set order_id = (select RAND()*30); #error 3 not declared
  set counter = ( select count(*) from Table_1 where order_id = Qid2);
  while counter!=0 do
    #Begin      #error 4 not required
      set Qid2 = (select RAND()*30);
       set counter =( select count(*) from Table_1 where order_id = Qid2);
    End while;
  #update Table_1
  # set order_id = Qid2  where current of c; #error 5 where? commented out
  fetch next from c into Qid2 ; #error 6 into missing.


End while;
close c;

end $$
delimiter ;  

答案 1 :(得分:0)

.App {
  text-align: center;
}

.App-logo {
  animation: App-logo-spin infinite 20s linear;
  height: 40vmin;
}

.App-header {
  background-color: #282c34;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  font-size: calc(10px + 2vmin);
  color: white;
}

.App-link {
  color: #61dafb;
}

@keyframes App-logo-spin {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}

/* Style the buttons that are used to open and close the accordion panel */
.accordion {
  background-color: #eee;
  color: #444;
  cursor: pointer;
  padding: 18px;
  width: 100%;
  text-align: left;
  border: none;
  outline: none;
  transition: 0.4s;
}

/* Add a background color to the button if it is clicked on (add the .active class with JS), and when you move the mouse over it (hover) */
.active, .accordion:hover {
  background-color: #ccc;
}

/* Style the accordion panel. Note: hidden by default */
.panel {
  padding: 0 18px;
  background-color: white;
  display: none;
  overflow: hidden;
}