我有一个将数据加载到临时表中的查询
SELECT * INTO #tempTable FROM Players where [reference month] = '2016-08-01'
我现在需要使用这些ID在上个月找到它们
SELECT ID FROM Players WHERE [reference month] = '2016-07-01' AND EXISTS
(SELECT #tempTable.ID FROM #tempTable)
我测试了#tempTable
(SELECT * FROM #tempTable),它返回了346条正确的记录。
以上代码正在搜索“玩家”表格中的每条记录(1000+),而不是#tempTable
如何修复此查询以仅使用#tempTable中的ID?
答案 0 :(得分:3)
您可以尝试以下任何选项,
SELECT ID
FROM Players
WHERE [reference month] = '2016-07-01'
AND EXISTS (
SELECT T.ID
FROM #tempTable T
WHERE T.ID = Players.ID
)
OR
SELECT P.ID
FROM Players P
INNER JOIN #tempTable T
ON T.ID = Players.ID
WHERE P.[reference month] = '2016-07-01'
答案 1 :(得分:1)
试试这个
SELECT ID
FROM Players
WHERE [reference month] = '2016-07-01'
AND ID IN (
SELECT #tempTable.ID
FROM #tempTable
);
答案 2 :(得分:1)
你差不多了。在WHERE
EXISTS
条款
SELECT ID FROM Players WHERE [reference month] = '2016-07-01' AND EXISTS
(SELECT T.ID FROM #tempTable T WHERE T.ID = Players.ID)