简体   繁体   English

如何从一个与另一个表匹配的表中删除行?

[英]How to delete rows from one table matching another table?

I am trying to delete rows from one table. 我正在尝试从一个表中删除行。 So this what i have done so far. 所以这是我到目前为止所做的。 I imported a .CSV file which created a temp table. 我导入了创建临时表的.CSV文件。 I would like to delete the rows in my original table with matching with temp table. 我想删除与临时表匹配的原始表中的行。

I tried the following code : 我尝试了以下代码:

Delete From Table1 
Where postid and userid in (Select postid, userid 
                            from Table2)

but it does not work. 但它不起作用。

The goal is to delete rows in Table 1 using Table 2. 目标是使用表2删除表1中的行。

A simple INNER JOIN should do the job: 一个简单的INNER JOIN应该可以完成这项工作:

DELETE T1
FROM Table1 T1
INNER JOIN Table2 T2
ON T1.postid = T2.postid
AND T1.userid = T2.userid

This is just another funny way : 这只是另一种有趣的方式:

DELETE FROM Table1 
WHERE STR(postid) + STR(userid) 
IN (SELECT STR(postid) + STR(userid) FROM Table2)

As far as I understand you want to Delete from Table1 where you want to create a composite condition based on postid and userid the from Table2 the problem is that you subquery in(Select postid,userid from Table2) is not returning the correct value as to match the in condition please modify the query to 据我了解,您想从Table1中删除,而您要在其中基于postid和userid创建一个组合条件,则要从Table2中删除,问题是您的子查询(从Table2中选择postid,userid)没有返回正确的值符合条件,请将查询修改为

DELETE T1
FROM Table1 T1
INNER JOIN Table2 T2
ON T1.postid = T2.postid
AND T1.userid = T2.userid

as stated in the previous answer 如上一个答案所述

You can use a Merge statement. 您可以使用Merge语句。 In this case you're only interested in deletes. 在这种情况下,您只对删除感兴趣。 A semicolon is required after the delete section, followed by a final semicolon to end the merge statement. 在delete节之后需要使用分号,然后使用最后一个分号来结束merge语句。 For example: 例如:

MERGE table1 AS target USING table2 AS source
ON target.postid = source.postid and target.userid = source.userid
WHEN Matched THEN DELETE;;

An example sqlfiddle: http://sqlfiddle.com/#!18/a932d/1/0 sqlfiddle示例: http ://sqlfiddle.com/#!18/a932d/1/0

The Merge documentation: https://docs.microsoft.com/en-us/sql/t-sql/statements/merge-transact-sql 合并文档: https : //docs.microsoft.com/zh-cn/sql/t-sql/statements/merge-transact-sql

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从表中删除一行,而在另一表上删除多行相关? - How to delete one row from a table and multiple rows on another that are related? 如何从与主表连接的另一表中删除所有行? - How to delete all rows from another table that are connected with the main one? 如何根据匹配的ID和日期比较删除一个表中的行 - How to delete rows in one table based on matching ids and date comparison 如何从 SQLITE 中的另一个表中删除行? - How to delete rows from another table in SQLITE? 如何从第一个表中选择行并在另一个表中匹配条目? - How to select rows from first table & matching entry in another table? 有效地从一个表中删除与另一个表不匹配的行 [MySQL] - Efficiently deleting rows from one table where not matching another [MySQL] 如何从 DB2 中的另一个表中存在行的表中删除? - How can I delete from one table where rows exist in another table in DB2? 如何在一个表中对MYSQL中的一系列行求和,匹配另一个表中的信息以获得结果 - How to Sum a series of rows in MYSQL from one Table, matching the info from another to get the result 如何通过具有特殊条件的另一个表从表中删除行? - How to delete rows from a table by another table with a special condition? 从一个不在其中的表中删除行(另一个表) - Delete rows from a table with not in ( another table )
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM