我正在尝试从一个表中删除行。所以这就是我到目前为止所做的。我导入了一个创建临时表的.CSV文件。我想删除原始表中与temp table匹配的行。
我尝试了以下代码:
Delete From Table1
Where postid and userid in (Select postid, userid
from Table2)
但它不起作用。
目标是使用表2删除表1中的行。
答案 0 :(得分:5)
一个简单的INNER JOIN
应该完成这项工作:
DELETE T1
FROM Table1 T1
INNER JOIN Table2 T2
ON T1.postid = T2.postid
AND T1.userid = T2.userid
答案 1 :(得分:1)
这只是另一种有趣的方式:
DELETE FROM Table1
WHERE STR(postid) + STR(userid)
IN (SELECT STR(postid) + STR(userid) FROM Table2)
答案 2 :(得分:0)
据我所知,你想要从Table1中删除你要根据postid和userid创建一个复合条件来自Table2,问题在于你的子查询(选择postid,userid来自 表2)没有返回正确的值以匹配in条件请将查询修改为
DELETE T1
FROM Table1 T1
INNER JOIN Table2 T2
ON T1.postid = T2.postid
AND T1.userid = T2.userid
如前一个回答所述
答案 3 :(得分:0)
您可以使用合并声明。在这种情况下,您只对删除感兴趣。删除部分后需要分号,后跟最后一个分号以结束合并语句。例如:
commit_data = json.loads(commit_json)
# open a file for writing
header = ['sha', 'author', 'committer']
with open('raw_commit_data.csv', 'w',encoding='utf-8') as commit_file:
csvwriter = csv.writer(commit_file)
csvwriter.writerow(header)
for commit in commit_data:
# Parse the objects
sha = commit['sha']
author = commit['author']['name']
committer = commit['committer']['name']
csvwriter.writerow([sha, author, committer])
示例sqlfiddle:http://sqlfiddle.com/#!18/a932d/1/0
合并文档:https://docs.microsoft.com/en-us/sql/t-sql/statements/merge-transact-sql