Search code examples
sqlitepeewee

How to select entries from one table where a certain field is not existing in the other table


How to select entries in peewee ORM from one table where a certain field is not in the other table?

E.g. How to count the employees which is there in tblemployees and not in tblcards?

select count(*) from tblEmployees 
   where name not in 
   (select employname from tblCards);

Note: Both fields, "name" and "employname" are not the respective primary/foreign keys).


Solution

  • You should provide your model definitions. But did you try something like:

    Employee.select().where(Employee.name.not_in(Card.select(Card.employname))).count()