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

  1. Newsgroups: comp.databases
  2. Path: sparky!uunet!gatech!darwin.sura.net!newsserver.jvnc.net!newsserver.technet.sg!nuscc!CHANHC@iscs.nus.sg
  3. From: chanhc@iscs.nus.sg
  4. Subject: Re: Help with SQL statement
  5. Message-ID: <1993Jan26.014419.4051@nuscc.nus.sg>
  6. Sender: usenet@nuscc.nus.sg
  7. Reply-To: CHANHC@NUSDISCS.BITNET
  8. Organization: Dept. of Information Systems and Computer Science, National University of Singapore
  9. References: <1993Jan22.211051.4867@vpnet.chi.il.us>
  10. Distribution: na
  11. Date: Tue, 26 Jan 1993 01:44:19 GMT
  12. Lines: 33
  13.  
  14. In article <1993Jan22.211051.4867@vpnet.chi.il.us>, mike@vpnet.chi.il.us 
  15. (michael kamlet) writes:
  16. >I'm having a problem writing an SQL statement for the following situation
  17. >(and I'm not sure its really possible)
  18. >
  19. >I have 2 tables lets say I have a Customer table and an order table.
  20. >I want to query the database for all customers and show their orders
  21. >for the past month.  I may have more than 1 order for a customer.
  22. >
  23. >The problem is that if I don't have an order for a customer, the join will
  24. >not find a match and that customer will be left out of the report.
  25. >
  26. >I still want to show the customer even if no orders are present.
  27. >
  28. >Is this possible with SQL ??  was my explanation clear enough ??
  29. >
  30. >Thanks 
  31. >
  32. >   Mike Kamlet
  33. >   mike@vpnet.chi.il.us
  34. You need to union the normal answer with the answer from a query
  35. that finds the customers with no orders, e.g.
  36.     select cust_num, cust_name, order_num, order_item
  37.     from customer, order
  38.     where  cust_num = order.cnum
  39.     union
  40.     select cust_num, cust_name, '    ', '    '
  41.     from  customer
  42.     where not exists  (select  order_num
  43.                 from order
  44.                 where  order.cnum = cust_num)
  45.  
  46. >
  47.