it's explained pretty well on the book
http://web2py.com/books/default/chapter/29/06/the-database-abstraction-layer#belongs
let me know if something is not clear
Il giorno mercoledì 25 settembre 2013 07:11:27 UTC+2, step ha scritto:
>
> Niphlod, can you please explain why you're using method _select instead of
> select in your code below? In there no need to connect to SQL to set
> all_courses?
>
> On Tuesday, September 24, 2013 3:35:34 PM UTC+2, Niphlod wrote:
>>
>> you can do that with
>>
>> all_courses = db(db.courses.id>0)._select(db.courses.memberid)
>> all_members_not_in_courses = db(~db.members.id.belongs(all_courses))
>>
>> you can eventually throw in a distinct on db.courses.memberid if the
>> number of courses rows is plenty.
>>
>> Il giorno martedì 24 settembre 2013 12:16:52 UTC+2, Michael Hall ha
>> scritto:
>>>
>>> I am not the most awesome SQL person and as such I am having trouble
>>> constructing a query I need.
>>>
>>> I have two tables in a database called "old". The parent table is called
>>> "members" and there is a child table called "courses".
>>>
>>> #--------
>>> old.define_table('members',
>>> Field('id'),
>>> Field('profile_id','string'),
>>> Field('title','string'),
>>> Field('firstname','string'),
>>> Field('surname','string'),
>>> Field('memberlevel','string'),
>>> Field('lawfirm','string'),
>>> Field('registerdate','date'),
>>> Field('expirydate','date'),
>>> Field('email','string'),
>>> Field('telno','string'),
>>> Field('address1','string'),
>>> Field('address2','string'),
>>> Field('address3','string'),
>>> Field('town','string'),
>>> Field('county','string'),
>>> Field('postcode','string'),
>>> Field('country','string'),
>>> Field('notes','text'),
>>> Field('memberstatus','string'),
>>> migrate=False)
>>>
>>> old.define_table('courses',
>>> Field('memberid',type='reference members',),
>>> Field('profile_id','string'),
>>> Field('course','string'),
>>> Field('enroldate','string'),
>>> migrate=False)
>>>
>>> I would like to select all records in the members table that are not
>>> referenced by records in the courses table. I suppose it could be described
>>> as the opposite of a join as I only want to select members that do not have
>>> courses attached to them.
>>>
>>> Any help would be appreciated.
>>>
>>>
>>>
--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.