Results 1 to 11 of 11

Thread: Another problem

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 1999
    Location
    Ilirska Bistrica, Slovenia
    Posts
    242

    Arrow

    Hello!

    I'm not very good in SQL statements, so I need someone to help me.
    I have three tables in database. Let's say that tables are named ONE, TWO and THREE and database is named DB.mdb. In first table I have customer info (ID, name, address...), in second is bill info (ID, CustomerID, payment...) and in third are things that are on bill with certain ID (ID, BillID, Qty, Price...). Now I need one recordset that would return all the info from three tables.
    Can you help me?
    Zvonko Bostjancic
    Ilirska Bistrica, Slovenia
    [email protected]
    Using VS6 Professional with SP3
    Programming mostly in VB and I've started to learn VC++ & MFC

  2. #2
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Lightbulb

    Can you clarify your table structure a bit?

    Your customer table must have a primary key CustomerID.
    Your bill table will also have a primary key BillID.
    Now, what do you have a third table for? If you want to track bill-wise details, they are available from the Bills table. You can also store the CustomerID in the Bills table, so that you can know which bill was issued to which customer. In this kind of structure, the SQL statement would look like:

    Select * from Bills where CustomerID = 'A0001'

    or if you want multiple values of CustomerID, then:

    Select * from Bills where CustomerID in ('A0001','B0003','H0123')

    If you can give details of the primary keys and foreign key references in the three tables you mentioned, I can give you a more specific answer.

    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  3. #3
    Hyperactive Member Paul Warren's Avatar
    Join Date
    Jun 2000
    Location
    UK
    Posts
    282

    Thumbs up SQL

    Zvonko - here's an SQL statement which will pull back a list of all the customers, payments made and details of what's on the bill. The key is in the joins, these are outer joins but you could use inner if you just wanted those people who have made payments and received bills.

    SELECT ONE.Name, TWO.Payment, THREE.Qty, THREE.Price
    FROM (ONE LEFT JOIN TWO ON ONE.CustID = TWO.CustID) LEFT JOIN THREE ON TWO.ID = THREE.BillID;
    That's Mr Mullet to you, you mulletless wonder.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Aug 1999
    Location
    Ilirska Bistrica, Slovenia
    Posts
    242
    Thanks, Paul!

    I know just basics about SQL statements, nothing about JOINs. Can you explain me a little about inner and outer joins?
    Zvonko Bostjancic
    Ilirska Bistrica, Slovenia
    [email protected]
    Using VS6 Professional with SP3
    Programming mostly in VB and I've started to learn VC++ & MFC

  5. #5
    Guest
    Zvonko,

    maybe the easiest way to slowly get into SQL would be to install Access, create your tables there and produce queries with a reasonable nice wizzard interface. Then you can copy (and of course study) the SQL statements Access produced for you.

    pozdrav

    Sascha

  6. #6
    Hyperactive Member Paul Warren's Avatar
    Join Date
    Jun 2000
    Location
    UK
    Posts
    282
    sascha is right, that's how I produced the SQL statement I posted. It's probably the easiest way because Access will do error checking for you. The basics of joins are this :

    an inner join - you will only see the results from the two joined tables if the key values are the same. That's the keys you've used to join the tables. All other records in each table are ignored.

    an outer join - This will include all those returned by an inner join plus all unmatched records on either the left or right of the join. Hence, these are called left outer and right outer joins. In my example "... FROM (ONE LEFT JOIN TWO ON ONE.CustID = TWO.CustID) ..." will include all records in ONE ( left side of join ) and all those records which have a macthing CustID in TWO.

    There are plenty of tutorials out there on the web and SQL is not that difficult to pick up. Good luck
    That's Mr Mullet to you, you mulletless wonder.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Aug 1999
    Location
    Ilirska Bistrica, Slovenia
    Posts
    242
    Thanks, guys!

    One question for sascha: I noticed that you speak slovene. Where are you from?
    Zvonko Bostjancic
    Ilirska Bistrica, Slovenia
    [email protected]
    Using VS6 Professional with SP3
    Programming mostly in VB and I've started to learn VC++ & MFC

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Aug 1999
    Location
    Ilirska Bistrica, Slovenia
    Posts
    242
    Oh, I forgot before: Where can I get examples (tutorials) about SQL statements?
    Zvonko Bostjancic
    Ilirska Bistrica, Slovenia
    [email protected]
    Using VS6 Professional with SP3
    Programming mostly in VB and I've started to learn VC++ & MFC

  9. #9

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Aug 1999
    Location
    Ilirska Bistrica, Slovenia
    Posts
    242
    Thanks, Clunietp!
    Zvonko Bostjancic
    Ilirska Bistrica, Slovenia
    [email protected]
    Using VS6 Professional with SP3
    Programming mostly in VB and I've started to learn VC++ & MFC

  11. #11
    Guest
    Zvonko,

    I am from Vienna, and to be honest, I studied Serbo-Croatian, so my skills in Slovenian are limited. I understand it a bit, but I cannot speek (although Croatians say, that I speak Croatian with a Slovenian acceent ).

    best regards

    Sascha


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width