简体   繁体   English

使用内部联接插入记录后更新表

[英]Update a table after inserting records with an Inner Join

I insert into TableA using a Select/Inner Join from TableB and TableC. 我使用来自TableB和TableC的“选择/内部联接”插入TableA。

Insert into TableA (C,S,M,C100)
SELECT C,S,M,group_concat(CID) FROM TableB
INNER JOIN TableC
ON TableB.CID= TableC.CID and P>=100  group by C,S,M

Now I need to update those records in two ways. 现在,我需要以两种方式更新这些记录。 One is identical to the first but now I want to update a different field with P<100, in essence: 一个与第一个相同,但是现在我想用P <100更新另一个字段,本质上是:

Insert into TableA (C,S,M,C0)
SELECT C,S,M,group_concat(CID) FROM TableB
INNER JOIN TableC
ON TableB.CID= TableC.CID and P<100  group by C,S,M

Except I don't want new records I want to update where TableA C,S,M match 除了我不想要新记录,我想在TableA C,S,M匹配的地方更新

The second thing I want to do is similar, but involves updating from a different table but in almost an identical manner 我想做的第二件事是类似的,但是涉及从另一个表进行更新,但几乎采用相同的方式

Insert into TableA (C,S,M,C100)
SELECT C,S,M,group_concat(CID) FROM TableD
INNER JOIN TableE
ON TableD.CID= TableD.CID and P>=100  group by C,S,M

In other words I could create each pass as separate inserts but would end up with duplicate records of C,S,M. 换句话说,我可以将每个遍创建为单独的插入,但是最终会得到C,S,M的重复记录。

Is there a way to do the passes after the first insert as Updates OR is there a way to do them each as Inserts and afterwards combine the records where C,S,M are identical? 有没有办法在第一次插入作为更新之后进行传递,或者有办法使它们分别作为插入,然后合并C,S,M相同的记录?

Use an update with join : 对join使用更新:

UPDATE TableA 
join (select C,S,M,group_concat(CID) as newCol
      FROM TableB
      where P<100
      group by C,S,M) t
 ON (tableA.c = t.c and tableA.s = t.s and TableA.M = t.m)
SET <YourColumn> = t.newCol

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

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