[英]Update two columns in MySQL from select statement
我正在嘗試更新 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 的例子,但我不能讓它在這里工作。 非常感謝您的幫助!
也許你應該分別設置它們的值,
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)
我想到了! MySQL 絕對可以一次更新多個列。 我需要更改我的原始查詢,因為 MySQL 需要在更新多列時使用 JOIN 和 ON。 為了證明這一點,我在示例中添加了一個額外的列“secondCol”。 我在網上到處找這個,一直沒有找到答案。 我希望這可以幫助一些人。
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.