SQL查询复制值 - 初学者

时间:2015-05-22 09:06:13

标签: mysql sql sqlite

INSERT INTO Odds_display (Team, Odds)
SELECT DISTINCT Team1, Odds1
FROM Odds
WHERE Team1 = 'Team SoloMid'
AND NOT EXISTS (SELECT Team FROM Odds_display)

所以,我不知道我是盲目还是愚蠢...或者两者兼而有之! SQL的初学者,但我的目标是从表中复制2个值并将它们插入到另一个表中,但不要插入任何重复项!

所以表格Odds是"original"表,我想复制到Odds_display表并防止重复!

2 个答案:

答案 0 :(得分:0)

您的子查询中需要WHERE子句来告诉您哪个团队不想创建

副本
INSERT INTO Odds_display (Team, Odds)
SELECT DISTINCT Team1, Odds1
FROM Odds
WHERE Team1 = 'Team SoloMid'
AND NOT EXISTS (SELECT * FROM Odds_display WHERE Team = 'Team SoloMid')

答案 1 :(得分:0)

尝试将NOT EXISTS更改为NOT IN,如下所示

INSERT INTO Odds_display (Team, Odds)
SELECT DISTINCT Team1, Odds1
FROM Odds
WHERE Team1 = 'Team SoloMid'
AND Team1 NOT IN (SELECT Team FROM Odds_display)