[英]Update two columns in MySQL from select statement
I'm trying to update two columns in MySQL:我正在尝试更新 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);
But, MySQL does not support this syntax.但是,MySQL 不支持这种语法。 I've seen some examples using JOIN, but I can't make it work here.
我看过一些使用 JOIN 的例子,但我不能让它在这里工作。 Many thanks for any help!
非常感谢您的帮助!
Maybe you should SET their values separately,也许你应该分别设置它们的值,
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)
I figured it out!我想到了! MySQL can definitely update more than one column at once.
MySQL 绝对可以一次更新多个列。 I needed to change my original query because MySQL requires using JOIN with ON when updating more than one column.
我需要更改我的原始查询,因为 MySQL 需要在更新多列时使用 JOIN 和 ON。 To prove it, I added an extra column "secondCol" to my example.
为了证明这一点,我在示例中添加了一个额外的列“secondCol”。 I have looked all over the web for this, and never found the answer.
我在网上到处找这个,一直没有找到答案。 I hope this helps some people.
我希望这可以帮助一些人。
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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.