| Guest | MySQL Error #1093 -
06-04-2007, 07:47 AM
Dear all,
I get an error #1093 when ever i attempt to execute an UPDATE Statement
with the following scenario:
CREATE DATABASE `college` DEFAULT CHARACTER SET utf8 COLLATE
utf8_general_ci;
use `college`;
CREATE TABLE `student` (
`student_id` SMALLINT UNSIGNED NOT NULL,
`student_name` VARCHAR(100) NOT NULL,
`student_avg_score` DECIMAL(10,9) UNSIGNED,
CONSTRAINT PK_student PRIMARY KEY (`student_id` ASC)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE INDEX IDX_student_1 ON `student` (`student_name` ASC);
CREATE INDEX IDX_student_2 ON `student` (`student_avg_score` DESC);
CREATE TABLE `subject` (
`subject_id` MEDIUMINT UNSIGNED NOT NULL,
`subject_avg_score` DECIMAL(10,9) UNSIGNED,
CONSTRAINT PK_subject PRIMARY KEY (`subject_id` ASC)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE INDEX IDX_subject_1 ON `subject` (`subject_avg_score` DESC);
CREATE TABLE `scores` (
`student_id` SMALLINT UNSIGNED NOT NULL,
`subject_id` MEDIUMINT UNSIGNED NOT NULL,
`score` TINYINT UNSIGNED NOT NULL,
CONSTRAINT PK_scores PRIMARY KEY (`student_id` ASC, `subject_id`
ASC)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE INDEX IDX_scores_1 ON `scores` (`score` DESC);
ALTER TABLE `scores` ADD CONSTRAINT student_scores
FOREIGN KEY (`student_id`) REFERENCES `student` (`student_id`) ON
DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE `scores` ADD CONSTRAINT subject_scores
FOREIGN KEY (`subject_id`) REFERENCES `subject` (`subject_id`) ON
DELETE CASCADE ON UPDATE CASCADE;
INSERT INTO `subject` VALUES (1,NULL);
INSERT INTO `subject` VALUES (2,NULL);
INSERT INTO `student` VALUES (1,'Robert',NULL);
INSERT INTO `student` VALUES (2,'Michael',NULL);
INSERT INTO `student` VALUES (3,'Sandra',NULL);
INSERT INTO `student` VALUES (4,'Lee',NULL);
INSERT INTO `student` VALUES (5,'Ali',NULL);
INSERT INTO `student` VALUES (6,'Zimic',NULL);
INSERT INTO `student` VALUES (7,'Peter',NULL);
INSERT INTO `student` VALUES (8,'Lim',NULL);
INSERT INTO `student` VALUES (9,'Thomas',NULL);
INSERT INTO `scores` VALUES (1,1,9.2);
INSERT INTO `scores` VALUES (2,1,3);
INSERT INTO `scores` VALUES (3,1,7.46);
INSERT INTO `scores` VALUES (4,1,3.02);
INSERT INTO `scores` VALUES (5,1,8.8);
INSERT INTO `scores` VALUES (6,1,4.7205);
INSERT INTO `scores` VALUES (7,1,6.1);
INSERT INTO `scores` VALUES (8,1,5.5);
INSERT INTO `scores` VALUES (9,1,1.8);
INSERT INTO `scores` VALUES (2,2,4.5);
INSERT INTO `scores` VALUES (4,2,9.593);
INSERT INTO `scores` VALUES (5,2,7.23);
INSERT INTO `scores` VALUES (8,2,8.808);
INSERT INTO `scores` VALUES (9,2,9.7);
SELECT sb.subject_id, AVG(sc.score) AS 'Average Score'
FROM subject sb INNER JOIN scores sc ON sc.subject_id = sb.subject_id
GROUP BY sc.subject_id
ORDER BY 'Average Score' DESC;
# OK
# subject_id Average Score
# 2 8.2000
# 1 5.5556
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
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? |