Re: Conditional INNER JOIN

From: Date: Mon, 26 May 2008 18:27:17 +0000
Subject: Re: Conditional INNER JOIN
References: 1  Groups: php.db 
Request: Send a blank email to php-db+get-45006@lists.php.net to get a copy of this message
2008/5/24 Ron Piggott <ron.php@actsministries.org>: > Is it possible to do a conditional INNER JOIN ? > > So far I have > > SELECT * FROM ( shopping_cart_orders INNER JOIN > shopping_cart_sales_shipping_address ON > shopping_cart_orders.shipping_address_reference = > shopping_cart_sales_shipping_address.reference ) > > If shopping_cart_sales_shipping_address.same_as_customer has a value of > "0" I need to > > INNER JOIN shopping_cart_sales_billing_address WHERE > shopping_cart_sales_billing_address.shipping_address_reference = > shopping_cart_sales_shipping_address.reference > > otherwise the shipping address is the same as the billing address. > Suggestions? If the fields of both queries are the same, you can use UNION to collect all the records: SELECT * FROM ( SELECT * FROM shopping_cart_orders INNER JOIN shopping_cart_sales_shipping_address ON shopping_cart_orders.shipping_address_reference = shopping_cart_sales_shipping_address.reference WHERE shopping_cart_sales_shipping_address.same_as_customer <> 0 ) UNION ( SELECT * FROM shopping_cart_orders INNER JOIN shopping_cart_sales_billing_address ON shopping_cart_sales_billing_address.shipping_address_reference = shopping_cart_sales_shipping_address.reference WHERE shopping_cart_sales_shipping_address.same_as_customer = 0 ) Hope it helps Martin

« previous php.db (#45006) next »