简体   繁体   English

根据表B(MySQL)的排序结果更新表A中的值

[英]Update values in Table A based on sorting results from Table B (MySQL)

First - Thanks in Advance for looking and possibly helping, it is most appreciated! 首先-在此先感谢您的帮助,我们非常感谢!

Basically I need to sort Table A, Column DESCRIPT, from A~Z and then... 基本上我需要从A〜Z对表A的列DESCRIPT进行排序,然后...

Based on this sort, then update Table B, Column PRINTORDER so that the lowest sort value would have a PRINTORDER = 1 and the highest sort value would have a PRINTORDER = 20,000 基于此排序,然后更新表B的列PRINTORDER,以使最低的排序值的PRINTORDER = 1,而最高的排序值的PRINTORDER = 20,000

In the subset of data the lowest is 18681 & highest is 18695. (Actual table range is from 1 to 20,000) 在数据子集中,最低的是18681,最高的是18695。(实际表格范围是1到20,000)

The only data that should be modified is PRINTORDER in Table B. 唯一应修改的数据是表B中的PRINTORDER。

Here is a subset of data from Table A: 这是表A中的数据子集:

    INUM    DESCRIPT
    23151   Crayon Apron
    23152   Acrylic bunny acry153
    23153   Acrylic easter egg acry154
    23154   Acrylic posypot tulip acrye01a
    23155   Acrylic orn chick acrye02
    23156   Hat baby chick bge10151
    23157   Sipper baby chick bge10158
    23158   Grow chick ea10991
    23159   Nail crystals easter ea11052
    23160   Mug jelly bean em11681
    23161   Plush tumbleweed chick he10148

Here is a subset of data from Table B: 这是表B中的数据子集:

    ID      INUM    PrintOrder
    142161  23151   18681
    144054  23161   18683
    145092  23159   18687
    145093  23160   18688
    145094  23152   18689
    145095  23153   18690
    145096  23155   18691
    145097  23154   18692
    145098  23158   18693
    145099  23156   18694
    145100  23157   18695

Here is the hoped for result: 这是希望的结果:

    ID      INUM    PrintOrder
    142161  23151   18681
    144054  23161   18694
    145092  23159   18693
    145093  23160   18692
    145094  23152   18689
    145095  23153   18683
    145096  23155   18687
    145097  23154   18688
    145098  23158   18690
    145099  23156   18691
    145100  23157   18695

Thanks & Happy New Year! 谢谢,新年快乐!

This might work. 这可能有效。 First you set the counter (i) which will go from 1 to 20000 (if you have 20000 rows). 首先,您设置计数器(i),它将从1到20000(如果您有20000行)。 Then you use the UPDATE ... JOIN ... ORDER BY pattern to sort by DESCRIPT, at which point you can set PrintOrder to the sequential number. 然后,您可以使用UPDATE ... JOIN ... ORDER BY模式以DESCRIPT进行排序,此时您可以将PrintOrder设置为序列号。 I hope it works for you. 我希望这个对你有用。

set @i:=1;

UPDATE b b
   JOIN 
   (
   SELECT a.INUM, DESCRIPT
   FROM a AS a
   WHERE DESCRIPT <> ''
   ORDER BY DESCRIPT
) a
ON b.INUM = a.INUM
SET b.PrintOrder=@i:=@i+1;

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

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