home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.databases
- Path: sparky!uunet!math.fu-berlin.de!ira.uka.de!scsing.switch.ch!univ-lyon1.fr!ghost.dsi.unimi.it!rpi!newsserver.pixel.kodak.com!laidbak!tellab5!odgate!mike
- From: mike@uunet!tellab5!odgate (Mike J. Kelly)
- Subject: Re: Help with SQL statement
- Message-ID: <1993Jan27.162831.7309@uunet!tellab5!odgate>
- Organization: Odesta Corporation
- References: <1993Jan22.211051.4867@vpnet.chi.il.us> <C1BqK7.FnH@watserv1.uwaterloo.ca>
- Distribution: na
- Date: Wed, 27 Jan 1993 16:28:31 GMT
- Lines: 39
-
- rpeterse@watserv1.uwaterloo.ca (Raymond Petersen) writes:
-
- >In article <1993Jan22.211051.4867@vpnet.chi.il.us> mike@vpnet.chi.il.us writes:
- >>I have 2 tables lets say I have a Customer table and an order table.
- >>I want to query the database for all customers and show their orders
- >>for the past month. I may have more than 1 order for a customer.
-
- >>The problem is that if I don't have an order for a customer, the join will
- >>not find a match and that customer will be left out of the report.
-
- >>I still want to show the customer even if no orders are present.
-
- >>Is this possible with SQL ?? was my explanation c
-
- If you database doesn't support outer joins (which synthesizes a row full
- of nulls as the join row from the orders table in this case if there is no
- matching customer_id in orders), you can probably work around it using
- UNION as follows:
-
- select <the columns from customers you want>, <the columns from orders you want>
- from customers c, orders o
- where c.customer_id = o.customer_id
- UNION
- select <the columns from customers you want>,NULL,NULL,NULL
- from customers
- where not exists ( select orders.customer_id from orders
- where orders.customer_id = customers.customer_id )
-
-
- Put one NULL in the second select statement for each column in the first
- select that comes from the orders table. This is one reason that some DB's
- don't support outer joins: you can usually roll your own.
-
-
- --
- --
- Mike Kelly Odesta Corporation, Northbrook, Illinois, USA
- ...!clout!odgate!mike - Until odesta.com is registered.
- odgate!mike@clout.uucp - From the Internet.
-