将mySQL数据库中的列更新1

zf9nrax1  于 2022-10-22  发布在  Mysql
关注(0)|答案(3)|浏览(187)

我有一个有250行的SQL数据库。我插入了一列0。我将第一行11行更新为1、2、3、4、5、6、7、8、9、10和11。其余的行仍然是0。
我将如何更新第11-250行,使其从0变为每个相关行值。
非常感谢。

r8xiu3jd

r8xiu3jd1#

有两种方法(我可以想到)来解决这个问题。您可以使用窗口函数,也可以使用变量。
假设您有以下测试表:

create table salsa (id int, name varchar(255), new_column int);

insert into salsa values
    (1, 'a', 0),
    (2, 'b', 0),
    (4, 'c', 0),
    (3, 'e', 0),
    (9, 'f', 0);

使用变量

变量一非常直接。尽管你对哪一个先发的控制权较小。此部件改编自row_number() in mysql

with
    my_ranking as (
      select s.*,
            @rownum := @rownum + 1 as _rank
     from salsa s,
          (select @rownum := 0) r
   )
update salsa as s1
   set new_column = (
     select _rank
     from my_ranking as s2
     where s1.id = s2.id
     limit 1
   );

@rownum变量用于跟踪行的当前秩。
参见小提琴:https://www.db-fiddle.com/f/Jmn5x34WXaBJq7oyemtXp/0

使用窗口函数

window functions附带rank(),我们将在本次使用。它至少需要mysql版本8才能使用。

with
   my_ranking as (
     select *,
            rank() over(order by id) as _rank
     from salsa
   )
update salsa as s1
   set new_column = (
     select _rank
     from my_ranking as s2
     where s1.id = s2.id
     limit 1
   );

我询问参考点如何对行进行排序的原因是,rank()需要分区才能工作,否则每个人都是排名1的。重要的部分是:

rank() over(order by id) as _rank

要确定行的“位置”,可以根据需要使用id或其他列。
参见小提琴:https://www.db-fiddle.com/f/nwLv9R7weQt4e5RhUbgaUL/0
注:

  • 上面的两个查询都需要mysql 8,因为我使用了CTE (Common Table Expression),因为我太懒了,无法将它们作为子查询编写。
  • 也许有更好的查询,可以把它想象成管道胶带之类的。
vdgimpew

vdgimpew2#

如果使用的是不支持CTE和窗口函数的经典版本(8.0之前),我们必须找到一个解决方法。为了解决这个问题,我们可以使用用户变量技巧。下面是演示它的完整步骤。

drop table if exists test;
create table test(id int primary key auto_increment, num int default 0);
delimiter //
drop procedure if exists makerows//
-- let's create a procedure to generate necessary rows.
create procedure makerows()
begin
declare c int default 1;

lp:loop
if c>250 then
leave lp;
end if;

if c<=11 then
insert test values(default,c);
else insert test (id) values(default);
end if;
set c=c+1;
end loop lp;

end//
delimiter ;
call makerows; -- now we have the test table with the id from 1 to 250 and the num from 1 to 11 with the rest being 0

-- here is the user variable trick to generate row id as required. Note we set the initial @row_id to -1 so we can calculate the first @row_id to be 0 for the first row (id=11).
select id ,@row_id:=@row_id+1 as row_id 
from test,(select @row_id:=-1) t 
where id>=11 order by id ;

-- the query above is used to get a derived table which shall be joined to the base table, so we can update the required num column 
update test t1 
join
(select id ,@row_id:=@row_id+1 as row_id 
from test,(select @row_id:=-1) t 
where id>=11 order by id) t2 
on t1.id=t2.id
set t1.num=t2.row_id
;

-- now we have the test table in which the id from 11 to 250 have the num column values from 0 to 239
select * from test;
1hdlvixo

1hdlvixo3#

创建一个用户定义的变量(例如@rownumber)以增加1的值。

UPDATE your_table
SET your_column = (@rownumber := 1 + @rownumber)
WHERE 0 = (@rownumber := 0)

结果:

| Input | Output |
|-------|--------|
| 1     | 1      |
| 2     | 2      |
| 3     | 3      |
| 4     | 4      |
| 5     | 5      |
| 6     | 6      |
| 7     | 7      |
| 8     | 8      |
| 9     | 9      |
| 10    | 10     |
| 11    | 11     |
| ...   | ...    |
| 0     | 245    |
| 0     | 246    |
| 0     | 247    |
| 0     | 248    |
| 0     | 249    |
| 0     | 250    |

演示

相关问题