![]() | |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
Dear all, |
|
UPDATE subject sb1 SET sb1.subject_avg_score = ( SELECT AVG(sc.score) FROM subject sb2 INNER JOIN scores sc ON sc.subject_id = sb2.subject_id WHERE sb2.subject_id = 1 GROUP BY sc.subject_id ) WHERE sb1.subject_id = 1; #1093 - You can\'t specify target table 'sb1' for update in FROM clause The average score displays normaly as illustrated but when attempted to set its vallue to the column `subject_avg_score`, which was left empty on purpose, then i always face this error! Any hints? |
#3
| |||
| |||
|
|
UPDATE subject sb1 SET sb1.subject_avg_score = ( SELECT AVG(sc.score) FROM subject sb2 INNER JOIN scores sc ON sc.subject_id = sb2.subject_id GROUP BY sc.subject_id ); #1093 - You can\'t specify target table 'sb1' for update in FROM clause Any hints? |
#4
| |||
| |||
|
|
coosa wrote: Dear all, [snip] UPDATE subject sb1 SET sb1.subject_avg_score = ( SELECT AVG(sc.score) FROM subject sb2 INNER JOIN scores sc ON sc.subject_id = sb2.subject_id WHERE sb2.subject_id = 1 GROUP BY sc.subject_id ) WHERE sb1.subject_id = 1; #1093 - You can\'t specify target table 'sb1' for update in FROM clause The average score displays normaly as illustrated but when attempted to set its vallue to the column `subject_avg_score`, which was left empty on purpose, then i always face this error! Any hints? Not sure why you would get this, but, have you tried making sb2 a view and using the view name? NOT TESTED: create view getavg as SELECT AVG(sc.score) as avg_score FROM subject sb2 INNER JOIN scores sc ON sc.subject_id = sb2.subject_id WHERE sb2.subject_id = 1 GROUP BY sc.subject_id UPDATE subject sb1 SET sb1.subject_avg_score = ( SELECT avg_score FROM getavg ) WHERE sb1.subject_id = 1; Also, what version/platform are you using IIRC there are some versions that did not support sub-selects such as this... -- Michael Austin Database Consultant Domain Registration and Linux/Windows Web Hosting Reseller http://www.spacelots.com |
#5
| |||
| |||
|
|
"coosa" <coosa76 (AT) gmail (DOT) com> wrote: [snip] UPDATE subject sb1 SET sb1.subject_avg_score = ( SELECT AVG(sc.score) FROM subject sb2 INNER JOIN scores sc ON sc.subject_id = sb2.subject_id GROUP BY sc.subject_id ); #1093 - You can\'t specify target table 'sb1' for update in FROM clause Any hints? RTFM. http://dev.mysql.com/doc/refman/5.0/...trictions.html "In general, you cannot modify a table and select from the same table in a subquery." XL -- Axel Schwenke, Support Engineer, MySQL AB Online User Manual: http://dev.mysql.com/doc/refman/5.0/en/ MySQL User Forums: http://forums.mysql.com/ |
#6
| |||
| |||
|
|
Axel Schwenke wrote: http://dev.mysql.com/doc/refman/5.0/...trictions.html Since you say "In general .." does it it mean it can be done some how? if yes, then how and thanks in advance. |
|
UPDATE subject sb1 SET sb1.subject_avg_score = ( SELECT AVG(sc.score) FROM subject sb2 INNER JOIN scores sc ON sc.subject_id = sb2.subject_id GROUP BY sc.subject_id ); |
![]() |
| Thread Tools | |
| Display Modes | |
| |