How to remove row that exists in another table?

那年仲夏 提交于 2019-12-07 03:29:02

问题


I have two tables. Main table is "CompleteEmailListJuly11" and the second table is "CurrentCustomersEmailJuly11". I want to delete rows in CompleteEmailListJuly11 table that CurrentCustomersEmailJuly11 has based off email.

I've tried this following Delete example, but it doesn't do anything close to what I'm trying to do. This only shows me the ones that EXIST in the database, it doesn't show me the the list of emails that AREN'T matching.

DELETE * FROM CompleteEmailListJuly11 AS i 
WHERE EXISTS ( 
    SELECT 1 FROM CurrentCustomersEmailJuly11 
    WHERE CurrentCustomersEmailJuly11.email = i.EmailAddress
)

Help is greatly appreciated.


回答1:


This is the query I think you need:

DELETE FROM CompleteEmailListJuly11
WHERE EmailAddress IN (SELECT email FROM CurrentCustomersEmailJuly11)

Ps: The DELETE query does not delete individual fields, only entire rows, so the * is not necessary, you will also need to "Execute" this query rather than "Previewing" or "Exporting"




回答2:


If you're building your DELETE query in Access' query designer, notice there are two different modes of operation which seem similar to "go ahead and do this".

  1. Datasheet View (represented by the grid icon labeled "View" on the "Design" section of the ribbon). That view enables you to preview the affected records, but does not actually delete them.
  2. The "Run" icon (represented by a red exclamation point). "Run" will actually execute the query and delete the affected records.

If you already know this, my description may seem insulting. Sorry. However, it seems that folks new to Access can easily overlook the distinction between them.




回答3:


You can use something like this adapted to delete

  SELECT ... // complete
  EXCEPT
  SELECT ... // current

I am not sure exactly how it maps to delete but take a look at that.

I fond it in a similar question: How do I 'subtract' sql tables?




回答4:


We can use Correlated Query to resolve the issue like

DELETE FROM COMPLETE C 
WHERE EMAIL = (SELECT EMAIL FROM CURR CU WHERE CU.EMAIL=C.EMAIL);


来源:https://stackoverflow.com/questions/11437677/how-to-remove-row-that-exists-in-another-table

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