[sqlalchemy] Compound Join

2011-01-27 Thread Eric N
I'm trying to construct a query where in the from clause I would end
up with something like
SELECT foo
FROM table1 JOIN
   table2 ON table1.id1 = table2.id1 JOIN
   table3 ON table1.id1=table3.id1 JOIN
   table4 ON table2.id2=table4.id2 AND table3.id3=table4.id3

I have tried various join combinations but I can only get it to join
table4 to table2 or table 3, not both.
Thanks,
- Eric

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



Re: [sqlalchemy] Compound Join

2011-01-27 Thread Michael Bayer

On Jan 27, 2011, at 8:06 PM, Eric N wrote:

 I'm trying to construct a query where in the from clause I would end
 up with something like
 SELECT foo
 FROM table1 JOIN
   table2 ON table1.id1 = table2.id1 JOIN
   table3 ON table1.id1=table3.id1 JOIN
   table4 ON table2.id2=table4.id2 AND table3.id3=table4.id3
 
 I have tried various join combinations but I can only get it to join
 table4 to table2 or table 3, not both.

the and_() function would be used as the onclause:

from sqlalchemy import and_

select = select.select_from(
table1.join(table2, table2.c.id1==table1.c.id1).\
join(table3, table1.c.id1==table3.c.id1).\
join(table4, and_(table2.c.id2==table4.c.id2, 
table3.c.id3==table4.c.id3))
)

You didn't say if you were using ORM or expression language, that above is 
expression language.  Same idea applies to ORM, use and_() in the ON clause. 

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.