我有這些SQL的指令:UPDATE,SUM SQL
CREATE TABLE discipline (
did INT PRIMARY KEY,
code VARCHAR(127) UNIQUE,
university VARCHAR(127),
number_students INT
) ENGINE=INNODB;
CREATE TABLE precedence(
type VARCHAR(127),
basic INT,
advanced INT,
PRIMARY KEY (basic, advanced),
FOREIGN KEY (basic) REFERENCES discipline(did) ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (advanced) REFERENCES discipline(did)
)ENGINE=INNODB;
INSERT INTO discipline VALUES (1,'BD','U1',30);
INSERT INTO discipline VALUES (2,'IBD','U2',30);
INSERT INTO discipline VALUES (3,'SIBD','U3',30);
INSERT INTO precedence VALUES ('optional',1,2);
INSERT INTO precedence VALUES ('mandatory',2,3);
UPDATE discipline SET did=did+did WHERE did=2;
我不理解爲什麼指令SELECT SUM (DISTINCT did)
的結果是6,而不是8(1 + 4 + 3)。具有更新指令的值2
(did)不應該是4
?你能解釋一下嗎?謝謝。
不應該預期的結果是** 8 **(1 + 3 + 4)而不是10?更新前的 – ppeterka
1 + 2 + 3 = 6。更新後1 + 4 + 3 = 8。你期待什麼? –
應該是8,它的工作對我來說至少在SQL SERVER – lante