[Durham] SQL LEFT JOIN help...

Brian Ronald brian at ppcis.org
Sun Nov 21 21:39:34 UTC 2010


On Sun, 21 Nov 2010 21:29:06 +0000 Richard Patterson
<Richard at helpquick.co.uk> wrote:
> > SELECT o.id, o.order_ref, con.contact_surname, com.company_name
> > FROM (
> > 	orders o
> > 	LEFT JOIN contacts con ON o.contact_id=con.id)
> > LEFT JOIN companies com ON con.company_id = com.id;
> 
> Ok, I have just tried:
> 
> SELECT o.id, o.order_ref, con.contact_surname, com.company_name
> FROM ((
> 	orders o
> 	LEFT JOIN contacts con ON o.contact_id=con.id)
> 		LEFT JOIN companies com ON con.company_id = com.id)
> 			LEFT JOIN addresses a ON o.address_id=a.id;
> 
> And that seems to do the trick... BUT is this the best way to do it?

If you're just after brevity, try:

SELECT o.id, o.order_ref, con.contact_surname, com.company_name FROM
orders o LEFT JOIN (contacts con, companies com, addresses a) ON
(o.contact_id=con.id AND con.company_id = com.id AND o.address_id=a.id);

Brian Ronald, Tyneside LUG
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 198 bytes
Desc: not available
URL: <http://mailman.lug.org.uk/pipermail/durham/attachments/20101121/687e5a03/attachment.pgp>


More information about the Durham mailing list