简体   繁体   English

如何以相反的顺序更新字段到MYSQL表中的其他字段

[英]how to update a field in reverse order to other field in a MYSQL table

I have a table with fields like this 我有一张桌子有这样的领域

lev_1_id,lev_1_seq,lev_1_new_seq,lev_2_id,lev_2_seq,lev_2_new_seq lev_1_id,lev_1_seq,lev_1_new_seq,lev_2_id,lev_2_seq,lev_2_new_seq

284e777e,1,null,b4dce5bb,1,null<br>
284e777e,1,null,dfd158ed,2,null<br>
284e777e,1,null,fedbf511,3,null<br>
0c7e0938,2,null,2333f431,1,null<br>
0c7e0938,2,null,808734fa,2,null<br>
0c7e0938,2,null,2504e0de,3,null<br>

And now I want to update the lev_1_new_seq and, lev_2_new_seq by reversing the values in lev_1_seq and lev_2_seq respectively. 现在,我想通过分别反转lev_1_seq和lev_2_seq中的值来更新lev_1_new_seq和lev_2_new_seq。

After updating the fields, the table should look like this: 更新字段后,该表应如下所示:

lev_1_id,lev_1_seq,lev_1_new_seq,lev_2_id,lev_2_seq,lev_2_new_seq lev_1_id,lev_1_seq,lev_1_new_seq,lev_2_id,lev_2_seq,lev_2_new_seq

284e777e,1,2,b4dce5bb,1,3<br>
284e777e,1,2,dfd158ed,2,2<br>
284e777e,1,2,fedbf511,3,1<br>
0c7e0938,2,1,2333f431,1,3<br>
0c7e0938,2,1,808734fa,2,2<br>
0c7e0938,2,1,2504e0de,3,1<br>

can anyone help me with updating the fields? 有人可以帮助我更新字段吗?

Thanks in advance! 提前致谢!

It appears that you should be able to calculate the "new" sequences by deducting the exiting sequences from the relevant maximums + 1. 看起来您应该能够通过从相关最大值+ 1中减去现有序列来计算“新”序列。

eg the max(lev_1_seq) + 1 = 3, so for an existing value of 2: 3-2 = 1, for an existing value of 1: 3-1 = 2 例如max(lev_1_seq)+ 1 = 3,因此对于现有值2:3-2 = 1,对于现有值1:3-1 = 2

UPDATE table1 tu
JOIN (
      SELECT
            t1.lev_1_id
          , m1.lev1maxseq
          , MAX(t1.lev_2_seq) + 1 lev2maxseq
      FROM table1 t1
            CROSS JOIN (
                  SELECT
                        MAX(lev_1_seq) + 1 lev1maxseq
                  FROM table1
            ) m1
      GROUP BY
            t1.lev_1_id
          , m1.lev1maxseq
      ) nv on tu.lev_1_id = nv.lev_1_id
SET
    tu.lev_1_new_seq = (nv.lev1maxseq - tu.lev_1_seq)
  , tu.lev_2_new_seq = (nv.lev2maxseq - tu.lev_2_seq)
;

see this sqlfiddle 看到这个sqlfiddle

results: 结果:

| lev_1_id | lev_1_seq | lev_1_new_seq | lev_2_id | lev_2_seq | lev_2_new_seq |
|----------|-----------|---------------|----------|-----------|---------------|
| 284e777e |         1 |             2 | b4dce5bb |         1 |             3 |
| 284e777e |         1 |             2 | dfd158ed |         2 |             2 |
| 284e777e |         1 |             2 | fedbf511 |         3 |             1 |
| 0c7e0938 |         2 |             1 | 2333f431 |         1 |             3 |
| 0c7e0938 |         2 |             1 | 808734fa |         2 |             2 |
| 0c7e0938 |         2 |             1 | 2504e0de |         3 |             1 |

This solution works if initial ordering was done by id. 如果初始排序是通过id完成的,则此解决方案有效。 2 - the column you updating, 1 - column that needs to be in reverse order, tmp - table, id - unique key, initial sorting column. 2您更新的列, 1需要相反顺序的列, tmp表, id唯一键,初始排序列。

BEGIN
DECLARE p INT;
DECLARE v INT;
DECLARE n INT DEFAULT 0;
DECLARE i INT DEFAULT 0;
SELECT COUNT(*) FROM `tmp` INTO n;
SET i=0;
WHILE i<n DO 
  select `1` from `tmp` order by `id` desc limit i,1 into p;
  select `id` from `tmp` order by `id` limit i,1 into v;

  update `tmp` set `2` = p where `id` = v;
  SET i = i + 1;
END WHILE;
End

For your table: 1 = lev_1_seq, 2 = lev_1_new_seq, id = lev_1_id 对于您的表: 1 = lev_1_seq, 2 = lev_1_new_seq, id = lev_1_id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM