使用从另一个表转置的列更新一个表

huwehgph  于 2021-06-24  发布在  Mysql
关注(0)|答案(2)|浏览(333)

我在更新从另一个表转置列的表时遇到问题。我在这里调查,接近解决方案,但我仍然没有击中重点。
我有一张table tbl_g08t1 以下列以及为简洁起见未包括的其他列(第一行:列名):

shl62 carrno    typ stat    50      71      72      73      74      75      76
747  35712528   0   6       29  (NULL)  (NULL)  (NULL)  (NULL)  (NULL)  (NULL)
747  35722615   0   6       29  (NULL)  (NULL)  (NULL)  (NULL)  (NULL)  (NULL)
747  35722625   0   6       29  (NULL)  (NULL)  (NULL)  (NULL)  (NULL)  (NULL)
747  35722664   0   6       29  (NULL)  (NULL)  (NULL)  (NULL)  (NULL)  (NULL)
821  35136730   0   8     (NULL)(NULL)  (NULL)  (NULL)  (NULL)  (NULL)  (NULL)
821  35722678   0   8       29  (NULL)  (NULL)  (NULL)  (NULL)  (NULL)  (NULL)
821  35833255   0   6     (NULL)(NULL)  (NULL)  (NULL)  (NULL)  (NULL)  (NULL)

我还有第二张table tbl_s80t1 对于自由文本,它看起来像:

sysfromt    sysshort freecode freetext
G08T1       35722652    98      101
G08T1       35722652    132     KCF9F27                                                               
G08T1       35722664    50      29
G08T1       35722664    71      20171004
G08T1       35722664    74      01Y                                                                   
G08T1       35722664    75      3
G08T1       35722664    76      A17108176                                                             
G08T1       35722664    97      8397080
G08T1       35722664    98      101
G08T1       35722664    132     KCF9F13                                                               
G08T1       35722678    50      29
G08T1       35722678    71      20171005
G08T1       35722678    74      01Y                                                                   
G08T1       35722678    75      3
G08T1       35722678    76      D1718496                                                              
G08T1       35722678    97      8395896
G08T1       35722678    98      101
G08T1       35722678    132     KCF9F27                                                               
G08T1       35722684    8       2017-10-05 09:53 C:3 Out:9 General fault!                             
G08T1       35722684    8       2017-10-05 09:54 C:3 Out:9 General fault!                             
G08T1       35722684    50      29

我想把这两张table合并起来

`tbl_g08t1`.`carrno` = `tbl_s80t1`.`sysshort` AND `tbl_s80t1`.`sysfromt` = 'G08T1'

并根据 freecode ,我得到了以下代码:

USE general_db;
    UPDATE tbl_g08t1
    LEFT JOIN `tbl_s80t1` ON (`tbl_g08t1`.`carrno` = `tbl_s80t1`.`sysshort` AND
              `tbl_s80t1`.`sysfromt` = 'G08T1')
      SET
         `50` = (SELECT MAX(`tbl_s80t1`.`freetext`) WHERE `tbl_s80t1`.`freecode` = 50),
         `71` = (SELECT MAX(`tbl_s80t1`.`freetext`) WHERE `tbl_s80t1`.`freecode` = 72),
         `72` = (SELECT MAX(`tbl_s80t1`.`freetext`) WHERE `tbl_s80t1`.`freecode` = 73),
         `76` = (SELECT MAX(`tbl_s80t1`.`freetext`) WHERE `tbl_s80t1`.`freecode` = 76)

问题是只有一列 50 已更新,所有其他 71...76 具有all(null)值。我本以为会的 freetext 价值观,当然 freecode 价值是相关的。
嗯,我不知道我是不是说得很清楚……还是说得更混乱了。。谢谢你的提示。
均线
ps当运行查询时我得到消息

33278 row(s) affected, 64 warning(s): 1265 Data truncated for column '50' at row 1
zzzyeukh

zzzyeukh1#

谢谢!真管用!我只需要改变柱子的长度 50...76 从varchar 50到255。在每个要删除的语句的末尾只有一个额外的“)”。执行时间相当不错:15秒更新>300万条记录。

2jcobegt

2jcobegt2#

问题出在你的set子句中。使用max()和select是一个子查询,不引用join子句。他们是独立的。我想这可能有用:

USE general_db;
UPDATE tbl_g08t1
    SET
     `50` = (SELECT MAX(`tbl_s80t1`.`freetext`) FROM tbl_s80t1 WHERE `tbl_s80t1`.`freecode` = 50 AND `tbl_g08t1`.`carrno` = `tbl_s80t1`.`sysshort` AND `tbl_s80t1`.`sysfromt` = 'G08T1')),
     `71` = (SELECT MAX(`tbl_s80t1`.`freetext`) FROM tbl_s80t1 WHERE `tbl_s80t1`.`freecode` = 71 AND `tbl_g08t1`.`carrno` = `tbl_s80t1`.`sysshort` AND `tbl_s80t1`.`sysfromt` = 'G08T1')),
     `72` = (SELECT MAX(`tbl_s80t1`.`freetext`) FROM tbl_s80t1 WHERE `tbl_s80t1`.`freecode` = 73 AND `tbl_g08t1`.`carrno` = `tbl_s80t1`.`sysshort` AND `tbl_s80t1`.`sysfromt` = 'G08T1')),
     `76` = (SELECT MAX(`tbl_s80t1`.`freetext`) FROM tbl_s80t1 WHERE `tbl_s80t1`.`freecode` = 76 AND `tbl_g08t1`.`carrno` = `tbl_s80t1`.`sysshort` AND `tbl_s80t1`.`sysfromt` = 'G08T1'))

如果没有sql小提琴,我就无法测试它。

相关问题