我的情況如下(SQLfiddle)給出:SQLite的更新多條記錄與不同的值由查詢
我有兩個表People
和Scores
,他們看起來是這樣的:
CREATE TABLE People
(
name TEXT,
minScore INTEGER,
maxScore INTEGER,
FOREIGN KEY (minScore) REFERENCES B(rowid),
FOREIGN KEY (maxScore) REFERENCES B(rowid)
);
INSERT INTO People (name) VALUES ('Terry');
INSERT INTO People (name) VALUES ('Bob');
CREATE TABLE Scores
(
nameID INTEGER,
score INTEGER,
FOREIGN KEY (nameID) REFERENCES A(rowid)
);
INSERT INTO Scores (nameID, score) VALUES (1, 10);
INSERT INTO Scores (nameID, score) VALUES (1, 8);
INSERT INTO Scores (nameID, score) VALUES (1, 5);
INSERT INTO Scores (nameID, score) VALUES (2, 12);
INSERT INTO Scores (nameID, score) VALUES (2, 8);
INSERT INTO Scores (nameID, score) VALUES (2, 2);
我想更新People
表以包含其minScore
和maxScore
的rowID
。我怎麼做?
目的:
Terry | 5 | 10
Bob | 2 | 12
這是我曾嘗試:
CREATE VIEW minScores
AS
SELECT Scores.rowid AS scoreID, Scores.nameID AS nameID
FROM Scores
WHERE score IN (SELECT MIN(score)
FROM Scores AS Scores2
GROUP BY Scores2.nameID);
UPDATE People
SET minScore = (SELECT scoreID FROM minScores)
WHERE People.rowid IN (SELECT nameID FROM minScores)
剛剛填充表的最後一個最小值。
(我不得不使用一個觀點,因爲我無法瞭解如何使用工作...)
謝謝你爲編輯@CL。這讀起來好多了。 – NE5534