将数据从MS SQL表中的2列复制到另一个表中的1列
我试图将tblFixtures中存储的比赛获胜者复制到tblEntrants中的单个列中。所以我基本上希望在tblEntrants中的AccountID列中创建一个获奖者列表,如下所示将数据从MS SQL表中的2列复制到另一个表中的1列,sql,sql-server,Sql,Sql Server,我试图将tblFixtures中存储的比赛获胜者复制到tblEntrants中的单个列中。所以我基本上希望在tblEntrants中的AccountID列中创建一个获奖者列表,如下所示 tblFixtures Columns: Player1 resultPLayer1 Player2 ResultPlayer2, CompID john 5 stu 2 (Guid)
tblFixtures Columns: Player1 resultPLayer1 Player2 ResultPlayer2, CompID
john 5 stu 2 (Guid)
dave 0 max 5 (Guid)
tblEntrants Columns: AccountID, CompID
john (Guid)
dave (Guid)
这是我到目前为止试过的,但不起作用
INSERT INTO tblEntrants(AccountID,compID) SELECT(CASE WHEN (SELECT resultplayer1 FROM tblfixtures) > (SELECT resultplayer2 FROM tblfixtures) THEN (SELECT player1 FROM tblfixtures) END), @compID
INSERT INTO tblEntrants(AccountID,compID) SELECT(CASE WHEN (SELECT resultplayer2 FROM tblfixtures) > (SELECT resultplayer2 FROM tblfixtures) THEN (SELECT player2 FROM tblfixtures) END), @compID
你可以用这个
INSERT INTO tblEntrants(AccountID,compID)
SELECT CASE WHEN ResultPlayer2 > resultPLayer1 THEN Player2 ELSE Player1 END, CompID
FROM tblfixtures
试试这个:您可以使用带有大小写的单个SELECT语句
非常感谢。我知道这很简单,但我还在这里学习。效果很好。
INSERT INTO tblEntrants(AccountID,compID)
SELECT
CASE WHEN resultPLayer1 > ResultPlayer2 THEN player1 ELSE player2 END, @compID
FROM tblFixtures