2010-06-04 83 views
0

我寫了這樣的命令來更新列在一個表中的列的平均另一個表..更新與平均數據的列其給出錯誤從另一個表列

UPDATE college_rating,products set 
property1_avg = avg(college_rating.rating1), 
property2_avg = avg(college_rating.rating2), 
property3_avg = avg(college_rating.rating3), 
property4_avg = avg(college_rating.rating4), 
property5_avg = avg(college_rating.rating5), 
property6_avg = avg(college_rating.rating6), 
property7_avg = avg(college_rating.rating7), 
property8_avg = avg(college_rating.rating8), 
property9_avg = avg(college_rating.rating9), 
property10_avg = avg(college_rating.rating10), 
property11_avg = avg(college_rating.rating11), 
property12_avg = avg(college_rating.rating12), 
property13_avg = avg(college_rating.rating13), 
property14_avg = avg(college_rating.rating14), 
property15_avg = avg(college_rating.rating15) 
where products.alias = concat(college_rating.property1,'-',college_rating.property2,'-',college_rating.property3) 
group by college_rating.property1,college_rating.property2, college_rating.property3 
+0

閱讀一行這樣的SQL查詢使得一種很難理解它。你能格式化它,使它變得更可讀嗎? – 2010-06-04 12:05:47

回答

1

MySQL多表更新語法不允許使用group by。

您可以通過將聚合移動到子查詢並加入多表更新中的子查詢來完成您正在嘗試執行的操作。

像這樣的東西應該工作:

update products p 
inner join (
    select concat(property1,'-',property2,'-',property3) as alias, 
    avg(rating1) as property1_avg, 
    avg(rating2) as property2_avg, 
    avg(rating3) as property3_avg, 
    avg(rating4) as property4_avg, 
    avg(rating5) as property5_avg, 
    avg(rating6) as property6_avg, 
    avg(rating7) as property7_avg, 
    avg(rating8) as property8_avg, 
    avg(rating9) as property9_avg, 
    avg(rating10) as property10_avg, 
    avg(rating11) as property11_avg, 
    avg(rating12) as property12_avg, 
    avg(rating13) as property13_avg, 
    avg(rating14) as property14_avg, 
    avg(rating15) as property15_avg 
    from college_rating 
    group by property1,property2, property3 
) as r on r.alias = p.alias 
set p.property1_avg = r.property1_avg, 
p.property2_avg = r.property2_avg, 
p.property3_avg = r.property3_avg, 
p.property4_avg = r.property4_avg, 
p.property5_avg = r.property5_avg, 
p.property6_avg = r.property6_avg, 
p.property7_avg = r.property7_avg, 
p.property8_avg = r.property8_avg, 
p.property9_avg = r.property9_avg, 
p.property10_avg = r.property10_avg, 
p.property11_avg = r.property11_avg, 
p.property12_avg = r.property12_avg, 
p.property13_avg = r.property13_avg, 
p.property14_avg = r.property14_avg, 
p.property15_avg = r.property15_avg; 
+0

@lke沃克 - nope老兄,它沒有工作...... 0行影響。 – Hacker 2010-06-07 09:10:10

+0

也許你的加入是錯誤的。沒有看到你的數據,我無法分辨。但是你可以嘗試用相同的連接做一個SELECT。如果返回多於0行,則更新應該匹配多於0行:'select count(*)from college_rating products.alias = concat(college_rating.property1,' - ',college_rating.property2,' - ',college_rating.property3)' – 2010-06-08 18:31:59

0

什麼是錯誤你得到?你需要有一個WHERE子句,除非你想更新查詢適用於所有的記錄

+0

我得到的錯誤就像#1064 - 你的SQL語法錯誤;檢查與您的MySQL服務器版本相對應的手冊,以便在第18行的'group by college_rating.property1,college_rating.property2,college_rating.prope'附近使用正確的語法 – Hacker 2010-06-07 11:44:03

+0

請發佈您的完整SQL語句,以便我和其他人可以查看並告訴你如何解決任何錯誤,如果有任何:) – 2010-06-08 05:22:00

0

我認爲你需要使用子查詢,我不確定你是否可以更新兩個表MySQL,至少不是沒有前綴的屬性。