有以下两个表和数据:
(1)person表
id | name | dept |
1 | allen | marketing |
2 | fiona | finance |
3 | richard | retail |
4 | lily | marketing |
(2)things表
id | name | owner |
1 | printer | allen |
2 | chair | allen |
3 | scanner | richard |
4 | desk |
|
5 | keyboard | fiona |
一旦条件中出现,不同两个表的字段做 == ,pyDAL 会自动进行 JOIN / INNER JOIN,
rows2 = db((db.things.owner == db.person.name) &
?????????????????? (db.person.dept == 'marketing')).select()
也可显性的写:
rows2 = db(db.person.dept == 'marketing').select(join=db.things.on(db.things.owner == db.person.name))
写成:join=db.things.on(db.person.name == db.things.owner)
????????? join=db.person.on(db.things.owner == db.person.name)
????????? join=db.person.on(db.person.name == db.things.owner)
都一样;
查询结果为:person表中marketing部门的有东西的人,他们有些什么东西:
1,allen,marketing, 1, printer_x, allen
1,allen,marketing, 2, chair_x,allen
我们希望:person表中marketing的人都列出来,不论他是否有东西
rows3 = db(db.person.dept == 'marketing').select(left=db.things.on(db.things.owner == db.person.name))
查询结果:
1,allen,marketing, 1, printer_x, allen
1,allen,marketing, 2, chair_x,allen
4,lily,marketing, null, null ,null
请特别注意语法:
left= 后面是?db.things.on(),写成这样效果也一样:
left=db.things.on(db.person.name == db.things.owner)