Results 1 to 7 of 7

Thread: [RESOLVED] Query Problem...

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Dec 2006
    Posts
    139

    Resolved [RESOLVED] Query Problem...

    I have a the following in a Database

    table1 : order
    table2 : ship

    order fields : userid,orderid etc...
    ship fields : userid,orderid etc..

    now i want a query such that.. select all orders from order table where order.userid = 1 and order.orderid = ship.orderid

    i did this in VB6 but gave error

    Code:
    select * FROM order WHERE order.userid = 1 and order.orderid = ship.orderid
    gives error

    please tell me correct statement..

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Query Problem...

    First off, is there a reason for Ship to contain the field UserID? I would assume that OrderID is unique within Order (ie: each OrderID only applies to one UserID), so Ship can simply link on that one field.


    As to the query, the problem is that you are trying to work with data that is not in your From clause - you have specified that you only want to work with data that is in the Order table.

    There are two ways to do this (either a join or a sub-query), but which you should use depends on what you are trying to achieve... are you trying to get a list of all fields in the Order table where at least one record exists in the Ship table with the same OrderID?

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Dec 2006
    Posts
    139

    Re: Query Problem...

    well, see.. I am populating a cmblist with all orderid.. CONDITION

    that should contain only those orderid which exist in both order and ship table i.e. all orderid will be in order table but NOT COMPULSORY to be in ship table.. basically if the order is not shipped it will not be there in ship table.. so i only want those orderid which have shipped also..

    the userid and orderid both exist in both the table "order" and "ship"

    both are unique.. i.e. there cannot be duplicate orderid for same user..

    and i only want to populate the list with the userid orders.. which userid is stored in a variable "userid" when he logs in

    on top i took "1" just as example

    so in the end.. i want to select all orderid of the user ( userid ) logged in which exist in order and ship table

    please help

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Query Problem...

    That's a very confusing explanation.. you basically said the OrderID has to be in both tables, but at the same time it doesn't have to be!

    If you want all OrderID's that exist in both tables (Order and Ship), you would use this:
    Code:
    select order.orderid FROM order INNER JOIN ship ON order.orderid = ship.orderid
    WHERE order.userid = 1
    If you want all OrderID's that exist in the Order table (whether they exist in Ship or not), you would use this:
    Code:
    select orderid FROM order WHERE order.userid = 1

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Dec 2006
    Posts
    139

    Re: Query Problem...

    works the 1st one.. thanks

    Just one more thing

    How about if i want only those orderID which DO NOT exist in ship ( just the opposite )

    what will be the change?

    Code:
    select order.orderid FROM order INNER JOIN ship ON order.orderid = ship.orderid
    will that = change to NOT = ???

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Query Problem...

    Good guess, but no - that does something very different indeed! It does an almost Cartesian join (very bad!).. it would return each value from Order X times, where X is the number of records in ship that aren't the same as it.

    Assume that order contains these OrderIDs: 1, 2
    ..and that Ship contains these: 1, 3
    ..it would return:
    1 (ship.orderid = 3)
    2 (ship.orderid = 1)
    2 (ship.orderid = 3)

    To do what you want, you need to use a sub-query to find all the values from Ship, and allow anything that isn't in that list:
    Code:
    SELECT order.orderid 
    FROM order 
    WHERE order.orderid NOT IN (SELECT ship.orderid FROM ship)

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Dec 2006
    Posts
    139

    Re: Query Problem...

    duh.. it was THAT simple.. dumb me..

    thankx a lot.. repped and resolved

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