简体   繁体   English

SQL:基于从另一个表中选择的列从表中删除行

[英]SQL: Deleting rows from table based on columns selected from another table

I'm trying to select rows in a table identified by the values in two columns. 我正在尝试选择由两列中的值标识的表中的行。 The values are found in another table. 这些值在另一个表中找到。 An example should help: 一个例子应该有帮助:

Table1 表格1

A | B | C
101 1   x
102 1   o
103 1   o

SELECT A, B FROM Table1 where Table1.C = 'o'

Returns: 返回:

A | B
102 1
103 1

I want to delete rows in Table2 where A and B match those returned by the query on Table1. 我想删除Table2中的行,其中A和B与Table1的查询返回的行匹配。

Table2 表2

A | B | D
101 1   Not deleted
102 1   Deleted
103 1   Deleted

Becomes: 变为:

A | B | D
101 1   Not selected

The first select works, but I can't see where to go from there. 第一次选择有效,但我看不到要去哪里。 I currently have the following, but it's throwing an error and I'm not clear what I should be doing. 我目前有以下内容,但它引发了错误,并且我不清楚我应该做什么。 Tutorials welcome as I'm new to SQL. 欢迎使用教程,因为我是SQL新手。

DELETE Table2
    where A, B in
        (SELECT A, B FROM Table1
    where Table1.C = 'o')

You can't specify more than 1 field in the where part when you are using the IN clause as in your query. 在查询中使用IN子句时,您不能在where部分中指定多个字段。 Instead, you can do this: 相反,您可以这样做:

delete t from 
table2 t
inner join table1 s on t.a = s.a and t.b = s.b
where s.c = 'o'

Demo 演示

暂无
暂无

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

相关问题 基于另一个表从表中删除一系列行作为 SQL 中的输入 - Deleting a range of rows from a table based on another table as input in SQL SQL Server根据来自另一个表的行旋转多个列 - SQL Server Pivot multiple columns based on rows from another table Oracle SQL 将行转换为列并根据另一个表中的条件为它们提供另一个表中的值 - Oracle SQL Converting Rows into Columns and Giving them Values from another Table based on Conditions from another Table 如何根据另一个表中多个列的值从一个表中提取多行,然后在 SQL 中连接? - How to extract multiple rows from a table based on values from multiple columns from another table and then concatenate in SQL? 根据特定条件从SQL表中删除行 - Deleting rows from a SQL table based on specific criteria PL SQL - 根据集合中的ID从表中删除行 - PL SQL - Deleting rows from table based on the ID in a collection SQL将一个表中的多行插入另一个表的列中 - SQL insert multiple rows from one table into columns on another table SQL - 根据另一个表将表中的行分配到不均匀的组中 - SQL - Distribute rows from a table into uneven groups, based on another table SQL:select 来自某个表的行,基于此表和另一个表中的条件 - SQL: select rows from a certain table based on conditions in this and another table 根据另一个表中的选定行从一个表中选择数据 - Select data from one table based on selected rows in another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM