home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / database / 9485 < prev    next >
Encoding:
Text File  |  1993-01-28  |  2.1 KB  |  51 lines

  1. Newsgroups: comp.databases
  2. 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
  3. From: mike@uunet!tellab5!odgate (Mike J. Kelly)
  4. Subject: Re: Help with SQL statement
  5. Message-ID: <1993Jan27.162831.7309@uunet!tellab5!odgate>
  6. Organization: Odesta Corporation
  7. References: <1993Jan22.211051.4867@vpnet.chi.il.us> <C1BqK7.FnH@watserv1.uwaterloo.ca>
  8. Distribution: na
  9. Date: Wed, 27 Jan 1993 16:28:31 GMT
  10. Lines: 39
  11.  
  12. rpeterse@watserv1.uwaterloo.ca (Raymond Petersen) writes:
  13.  
  14. >In article <1993Jan22.211051.4867@vpnet.chi.il.us> mike@vpnet.chi.il.us writes:
  15. >>I have 2 tables lets say I have a Customer table and an order table.
  16. >>I want to query the database for all customers and show their orders
  17. >>for the past month.  I may have more than 1 order for a customer.
  18.  
  19. >>The problem is that if I don't have an order for a customer, the join will
  20. >>not find a match and that customer will be left out of the report.
  21.  
  22. >>I still want to show the customer even if no orders are present.
  23.  
  24. >>Is this possible with SQL ??  was my explanation c
  25.  
  26. If you database doesn't support outer joins (which synthesizes a row full
  27. of nulls as the join row from the orders table in this case if there is no
  28. matching customer_id in orders), you can probably work around it using
  29. UNION as follows:
  30.  
  31. select <the columns from customers you want>, <the columns from orders you want>
  32. from customers c, orders o
  33. where  c.customer_id = o.customer_id
  34. UNION
  35. select <the columns from customers you want>,NULL,NULL,NULL
  36. from customers
  37. where not exists ( select orders.customer_id from orders
  38.            where orders.customer_id = customers.customer_id )
  39.  
  40.  
  41. Put one NULL in the second select statement for each column in the first
  42. select that comes from the orders table.  This is one reason that some DB's
  43. don't support outer joins: you can usually roll your own.
  44.  
  45.  
  46. -- 
  47. --
  48. Mike Kelly               Odesta Corporation, Northbrook, Illinois, USA
  49. ...!clout!odgate!mike    - Until odesta.com is registered.
  50. odgate!mike@clout.uucp   - From the Internet.
  51.