[英]SQL Server delete query involving two tables
So I have tables A and B in SQL Server, and columns a and b respectively.所以我在 SQL 服务器中有表 A 和 B,分别是 a 和 b 列。 I want to do the following in pseudo-query command, but I can't seem to figure it out.我想在伪查询命令中执行以下操作,但我似乎无法弄清楚。
I want to我想要
DELETE FROM A
WHERE a < 100 "and only if these selected (for deletion) values don't exist in column b in table B"
The reason is that I'm trying to delete some data from table A, but it is giving me an error saying that there is a constraint between values in Aa and Bb原因是我试图从表 A 中删除一些数据,但它给了我一个错误,说 Aa 和 Bb 中的值之间存在约束
Does this involve aliases?这是否涉及别名? It is confusing..很混乱。。
Try this if you are using SQL Server 2005 or newer:如果您使用的是 SQL Server 2005 或更新版本,请尝试以下操作:
DELETE FROM TableA
WHERE a < 100 AND
a NOT IN (SELECT B FROM TableB)
For SQL Server 2000 this should work:对于 SQL Server 2000 这应该可以工作:
DELETE ta
FROM TableA as ta
LEFT JOIN TableB as tb
ON ta.a = tb.b
WHERE ta.a < 100 AND tb.b IS NULL
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.