我有两个表都有PhraseId作为键:
Phrase containing
PhraseId, name and score column
PhraseSource containing
PhraseId and name column
在更新期间填充PhraseSource表,它可能包含更少,相同或更多行。
有没有办法
请注意,我需要在乐句表中保留乐谱数据。
答案 0 :(得分:0)
易:
DELETE FROM Phrase;
INSERT INTO Phrase SELECT * FROM PhraseSource;
但如果更新不小,那么按照描述更有效:
INSERT INTO Phrase
SELECT * FROM PhraseSource
WHERE PhraseId NOT IN (SELECT PhraseId FROM Phrase);
UPDATE Phrase
SET name = (SELECT name
FROM PhraseSource
WHERE PhraseId = Phrase.PhraseId)
WHERE name <> (SELECT name
FROM PhraseSource
WHERE PhraseId = Phrase.PhraseId);
DELETE FROM Phrase
WHERE PhraseId NOT IN (SELECT PhraseId FROM PhraseSource);