如何从SQL Server中具有多个值的2个复合键的表中删除多行?

时间:2023-01-31 09:18:35

I have one table that contains a list o telephones from organizations. This table has 2 columns a foreign key to the table Organizations (ID) and telephone.

我有一张表,其中包含来自组织的电话列表。此表有2列表,组织(ID)和电话的外键。

Telephones

ID  Telephone   
1   1234
1   3456
1   9999
3   9999

I want to delete the fist and second rows, for doing this I have the ID value 1 and a table containing the numbers to delete ( 1234, 3456) called @TempTels

我想删除第一行和第二行,为此,我有ID值1和一个包含要删除的数字的表(1234,3 3456),名为@TempTels

I thought of this but it didn't work :

我想到了这个,但它不起作用:

Delete From TelephonesOrg 
where ID = ID AND Telephone = (Select Telephone from @TempTels)

Thanks in advance for any help.

在此先感谢您的帮助。

2 个解决方案

#1


3  

Since that sub-select returns more than one value, you need to use IN - not an = equals sign:

由于该子选择返回多个值,因此您需要使用IN - 而不是= =等号:

Delete From TelephonesOrg 
where ID = 1 
  AND Telephone IN (Select Telephone from @TempTels)

#2


0  

@Dante Snow from your code you have 3 rows having id=1..so if you want to delete first 2 row only you have to use like below..

@Dante Snow从你的代码中你有3行id = 1..so如果你想删除前2行只有你必须使用如下所示..

Delete From TelephonesOrg 
where ID = 1 AND Telephone in (1234, 3456)

#1


3  

Since that sub-select returns more than one value, you need to use IN - not an = equals sign:

由于该子选择返回多个值,因此您需要使用IN - 而不是= =等号:

Delete From TelephonesOrg 
where ID = 1 
  AND Telephone IN (Select Telephone from @TempTels)

#2


0  

@Dante Snow from your code you have 3 rows having id=1..so if you want to delete first 2 row only you have to use like below..

@Dante Snow从你的代码中你有3行id = 1..so如果你想删除前2行只有你必须使用如下所示..

Delete From TelephonesOrg 
where ID = 1 AND Telephone in (1234, 3456)