SQL Delete with Subquery

纵然是瞬间 提交于 2020-01-05 08:33:34

问题


I'm trying to delete from a table with the results of a subquery. The results return a unique tuple, and currently I end up deleting more than just the results returned because i'm only checking col1 results.

DELETE FROM Table1 exTable
WHERE exTable.col1 = ... AND exTable.col2 = ...
(SELECT col1, col2
FROM ...)

回答1:


Use a join to match more than 1 column.

DELETE t1
FROM Table1 t1
inner join 
(
   select col1, col2 
   from other_table 
   where ...
) t2 on  t2.col1 = t1.col1 
     and t2.col2 = t1.col2


来源:https://stackoverflow.com/questions/17448044/sql-delete-with-subquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!