Sql

Discussion in 'Programming' started by Hade, Jan 3, 2008.

  1. #1
    I have the following tables:

    Table 1
    +------------+
    | id | name |
    +----+-------+
    | 1 | Bob |
    | 2 | Sue |
    | 3 | Jim |
    | 4 | Jen |
    +----+-------+

    Table 2
    +------------+----------+
    | id | manid | womanid |
    +----+-------+----------+
    | 1 | 3 | 2 |
    +----+-------+----------+


    I want to retreive the row from table 2 with the names Jim and Sue. How can I do this?

    Thanks!
     
    Hade, Jan 3, 2008 IP
  2. big.gamma

    big.gamma Peon

    Messages:
    144
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #2
    Try this one:

    
    SELECT * 
    FROM table2 
    WHERE 
    	table2.manid=(select id from table1 where name='Jim') 
    AND
    	table2.womanid=(select id from table1 where name='Sue');
    Code (markup):
     
    big.gamma, Jan 3, 2008 IP
  3. Hade

    Hade Active Member

    Messages:
    701
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    90
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #3
    Thanks for your help, but I don't know the names when I'm doing the SQL call. I simply want to retrieve names associated with the record in table2.
     
    Hade, Jan 3, 2008 IP
  4. AHA7

    AHA7 Peon

    Messages:
    445
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #4
    Get man id:
    SELECT name FROM table1 WHERE id = (SELECT manid from table2 WHERE id='1');
    Code (markup):
    Get woman id:
    SELECT name FROM table1 WHERE id = (SELECT womanid from table2 WHERE id='1');
    Code (markup):
     
    AHA7, Jan 3, 2008 IP