MySQL, eine 'alle ausser' Abfrage

Ich habe ein Problem

Nehmen wir an, ich habe 2 Tabellen, eine (accounts) mit ID und name und eine (ref) mit ID und ID_account.

Ich möchte nun eine Abfrage erstellen die mir möglichst effizient alle accounts aus accounts gibt, welche noch nicht in ref mit ihrer ID in ID_account vorkommen. In ref werden account id’s gespeichert, also ID ist fortlaufend und in ID_accounts kommt dann die ID des Accounts aus der Tabelle accounts.

In einfachen Worten:

Select * from accounts where „id noch nicht in ref.ID_account“

Und was nehme ich am besten bei where?

Ich bin total am verzweifeln weils einfach nicht klappen will…

Moin, Andi,

die Benamsung bringt mich ins schleudern, deshalb schlage ich neue Tabellen vor:

**accounts** (<u>account_id</u>, col1, col2, ...)



**refs** (<u>ref_id</u>, account\_id, col1, col2, ...)




 Select \* from accounts 
 where account\_id not in
 (select account\_id from refs) 

Gruß Ralf