从select语句更新MySQL中的两列

时间:2018-02-18 21:32:15

标签: mysql join sql-update

我试图在MySQL中更新两列:

update table1
set (table1.score, table1.count) = 
(select (table2.maxScore - table2.score ) as diff, count(*)
from table2
where (table2.maxScore - table2.score) <= 600
and table2.age > 50
group by diff);

但是,MySQL不支持这种语法。我已经看过一些使用JOIN的例子,但是我无法在这里工作。非常感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

也许你应该单独设置它们的值,

UPDATE scoresUniverse 
SET scoreUniver.GNfemScore = (Analytics.GN_recency_max_score - Analytics.GN_recency_score ),
ScoresUniverse.GNfemCount = (Select count(*) from Analytics.GN_recency_score where (Analytics.PR_recency_max_score - Analytics.PR_recency_score) <= 600 and Analytics.Older_female_50 > 0)

答案 1 :(得分:0)

我明白了! MySQL肯定可以一次更新多个列。我需要更改我的原始查询,因为MySQL在更新多个列时需要使用JOIN和ON。为了证明这一点,我添加了一个额外的列&#34; secondCol&#34;我的例子。我已经浏览了整个网络,但从未找到答案。我希望这可以帮助一些人。

 update table1 join
 (select (table2.maxScore - table2.score ) as diff, count(*) as count
 from table2
 where (table2.maxScore - table2.score) <= 600
 and table2.age > 50
 group by diff) as scores
 on table1.count = scores.count
 set table1.score = scores.diff, table1.secondCol = scores.count;