SQL query to find all records which are not in another table

40 views
Skip to first unread message

Vladimir Makarov

unread,
Nov 10, 2014, 2:45:43 PM11/10/14
to web...@googlegroups.com
I'm just a little bit confusing because I can't make correct selection to find records that are absent in another (referenced) table.
It's clear to me how to make inner join:
rows = db(db.persons).select(join=db.tbl.on(db.persons.id==db.tbl.person), groupby=db.persons.f_name, orderby=~db.persons.id)
But how to acheive the opposite sesult?
How to implement this statement?

SELECT * 
FROM   persons 
WHERE  id NOT IN (SELECT id FROM tbl.person)
Thanks!

Leonel Câmara

unread,
Nov 10, 2014, 2:56:17 PM11/10/14
to web...@googlegroups.com
You can use belongs. You ask for those that do not belong.

something like
tbl_person =  db()._select(db['tbl.person'].id)
db(~db.persons.id.belongs(tbl_person)).select()

Do you actually have dots in your tablename? That's a terrible idea.

Vladimir Makarov

unread,
Nov 11, 2014, 1:22:58 PM11/11/14
to web...@googlegroups.com
Hi, Leonel. Thank you for taking me to the right way.
It works well:
     rows = db(~db.persons.id.belongs(db()._select(db.tbl.person))).select()
And I don't use dots in table names.
Reply all
Reply to author
Forward
0 new messages