[英]SQL - delete record from table X if same record not in table Y
sorry to make a big fuss about delete same records again, but i can't find any solution :( 很抱歉再次删除相同的记录大惊小怪,但我找不到任何解决方案:(
i need something like that: 我需要这样的东西:
if a select of 3 columns values from table1 如果从table1中选择3列值
is NOT equal to the same select in table2 不等于table2中的相同选择
than delete record from table2 而不是从table2删除记录
that are my aproaches: 这是我的aproaches:
DELETE FROM table2
INNER JOIN table1
ON table2.basketid = table1.basketid AND
table2.artid = table1.artid
WHERE table1.userid='007'
DELETE FROM table2
WHERE NOT EXISTS (
SELECT basketid, artid
FROM table1
)
AND userid ='007'
Thanks for your help or any tipp!! 谢谢你的帮助或任何tipp !!
you can do this in SQL Server 您可以在SQL Server中执行此操作
DELETE t2
FROM Table2 t2
WHERE NOT EXISTS ( SELECT 1
FROM Table1 t1
WHERE t1.basketid = t2.basketid
AND t1.artid = t2.artid )
AND t2.userid = '007'
notice the filters in the select that compares the values in Table2 to Table1 注意select中用于比较Table2到Table1中的值的过滤器
if userid is also in Table2 you might want to add that to the WHERE also 如果userid也在Table2中,您可能还想将其添加到WHERE中
DELETE t2
FROM Table2 t2
WHERE NOT EXISTS ( SELECT 1
FROM Table1 t1
WHERE t1.userid = t2.userid
AND t1.basketid = t2.basketid
AND t1.artid = t2.artid )
AND t2.userid = '007'
Could be you need a where ( ) not in ( ) 可能你需要一个where()不在()
DELETE FROM table2
WHERE (basketid, artid ) NOT IN (
SELECT basketid, artid
FROM table1
)
AND userid ='007';
DELETE y
FROM X
LEFT JOIN Y
ON
X.COL1=Y.COL1
AND
X.COL2=Y.COL2
AND
X.COL3=Y.COL3
WHERE
Y.COL1 IS NULL
Try the above code. 试试上面的代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.