Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL填充空列_Mysql_Sql_Sql Update - Fatal编程技术网

MySQL填充空列

MySQL填充空列,mysql,sql,sql-update,Mysql,Sql,Sql Update,我有下表: MyTable(id、组列、列到列填充) 当前“column_to_fill”为空,我想用递增的整数填充它。对于组_列的每个值,值应从0开始 我的桌子看起来像这样: +-----------------------------------+------------+----------------+ | id |group_column| column_to_fill | +------------------------

我有下表: MyTable(id、组列、列到列填充)

当前“column_to_fill”为空,我想用递增的整数填充它。对于组_列的每个值,值应从0开始

我的桌子看起来像这样:

+-----------------------------------+------------+----------------+
|               id                  |group_column| column_to_fill |
+-----------------------------------+------------+----------------+
| 9b71dd5c-d8a6-461c-b1f3-af1e4b1d  | Value1     |     null       |
| 38886977-0f34-4059-b192-f94f5aed  | Value1     |     null       |
| d98e16da-a919-4242-baf8-dbbef636  | Value2     |     null       |
| e1ab88a9-3307-49a6-b37d-72cdb5da  | Value2     |     null       |
| 75174dcb-eb74-4c13-80a1-1b21905d  | Value2     |     null       |
+-----------------------------------+------------+----------------+
+-----------------------------------+------------+----------------+
|               id                  |group_column| column_to_fill |
+-----------------------------------+------------+----------------+
| 9b71dd5c-d8a6-461c-b1f3-af1e4b1d  | Value1     |     0          |
| 38886977-0f34-4059-b192-f94f5aed  | Value1     |     1          |
| d98e16da-a919-4242-baf8-dbbef636  | Value2     |     0          |
| e1ab88a9-3307-49a6-b37d-72cdb5da  | Value2     |     1          |
| 75174dcb-eb74-4c13-80a1-1b21905d  | Value2     |     2          |
+-----------------------------------+------------+----------------+
我希望它看起来像这样:

+-----------------------------------+------------+----------------+
|               id                  |group_column| column_to_fill |
+-----------------------------------+------------+----------------+
| 9b71dd5c-d8a6-461c-b1f3-af1e4b1d  | Value1     |     null       |
| 38886977-0f34-4059-b192-f94f5aed  | Value1     |     null       |
| d98e16da-a919-4242-baf8-dbbef636  | Value2     |     null       |
| e1ab88a9-3307-49a6-b37d-72cdb5da  | Value2     |     null       |
| 75174dcb-eb74-4c13-80a1-1b21905d  | Value2     |     null       |
+-----------------------------------+------------+----------------+
+-----------------------------------+------------+----------------+
|               id                  |group_column| column_to_fill |
+-----------------------------------+------------+----------------+
| 9b71dd5c-d8a6-461c-b1f3-af1e4b1d  | Value1     |     0          |
| 38886977-0f34-4059-b192-f94f5aed  | Value1     |     1          |
| d98e16da-a919-4242-baf8-dbbef636  | Value2     |     0          |
| e1ab88a9-3307-49a6-b37d-72cdb5da  | Value2     |     1          |
| 75174dcb-eb74-4c13-80a1-1b21905d  | Value2     |     2          |
+-----------------------------------+------------+----------------+

如何在MySQL中更新它?

您可以使用这个简单的
选择
(此外,您可以使用ut更新您的表):

更新
语句:

select @lag := '', @i := 0;
update tbl
join (
  select id,
         @lag,
         case when group_column = @lag then @i := @i + 1 else @i := 0 end rn,
         @lag := group_column, group_column
  from tbl
) t on tbl.id = t.id and tbl.group_column = t.group_column
set tbl.column_to_fill = t.rn

您可以使用此简单的
选择
(此外,您还可以使用ut更新表格)来实现相同的效果:

更新
语句:

select @lag := '', @i := 0;
update tbl
join (
  select id,
         @lag,
         case when group_column = @lag then @i := @i + 1 else @i := 0 end rn,
         @lag := group_column, group_column
  from tbl
) t on tbl.id = t.id and tbl.group_column = t.group_column
set tbl.column_to_fill = t.rn

我建议如下:

set @rn := -1;
set @g := '';

update t
    set column_to_fill = if(@g = group_column, @rn := @rn + 1,
                            if(@g := group_column, @rn := 0, @rn := 0)
                          )
    order by group_column;
在MySQL中使用变量时,记住表达式的求值顺序并没有保证,这一点非常重要。因此,需要将变量的所有引用和赋值放在同一个表达式中

在MySQL 8+中,您可以执行以下操作:

update t join
       (select t.*, row_number() over (partition by group_column order by group_column) as seqnum
        from t
       ) tt
       on t.id = tt.id
    set column_to_fill = seqnum - 1;

我建议如下:

set @rn := -1;
set @g := '';

update t
    set column_to_fill = if(@g = group_column, @rn := @rn + 1,
                            if(@g := group_column, @rn := 0, @rn := 0)
                          )
    order by group_column;
在MySQL中使用变量时,记住表达式的求值顺序并没有保证,这一点非常重要。因此,需要将变量的所有引用和赋值放在同一个表达式中

在MySQL 8+中,您可以执行以下操作:

update t join
       (select t.*, row_number() over (partition by group_column order by group_column) as seqnum
        from t
       ) tt
       on t.id = tt.id
    set column_to_fill = seqnum - 1;

可以使用循环创建存储过程。看看这个答案:您可以使用循环创建一个存储过程。看看这个答案: