Selecting data from two mysql tables.

Discussion in 'PHP' started by bobby9101, Dec 8, 2006.

  1. #1
    I need to use something like the:
    mysql_query(SELECT * FROM test1 WHERE user='test');
    However, the user field is in another table.
    How do I do this?
    PHP:

     
    bobby9101, Dec 8, 2006 IP
  2. PinoyIto

    PinoyIto Notable Member

    Messages:
    5,863
    Likes Received:
    170
    Best Answers:
    0
    Trophy Points:
    260
    #2
     
    PinoyIto, Dec 8, 2006 IP
  3. TheGuy

    TheGuy Peon

    Messages:
    138
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    What does your db structure looks like? Do you have any foreign key relationship between these two tables?
     
    TheGuy, Dec 8, 2006 IP
  4. CodyRo

    CodyRo Peon

    Messages:
    365
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Don't use sub-queries.. use JOIN's (check MySQL documentation)
     
    CodyRo, Dec 9, 2006 IP
  5. adsexper

    adsexper Peon

    Messages:
    19
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    it's hard to say without knowing what your tables contain but you basically need to join the table and do something like

    select test1.* from test1,test2 where test1.bla=test2.bla and test2.user='test'

    where test2 is your other table and bla is the field that you use to link the 2 tables.

    hope this helps,
     
    adsexper, Dec 9, 2006 IP
  6. bobby9101

    bobby9101 Peon

    Messages:
    3,292
    Likes Received:
    134
    Best Answers:
    0
    Trophy Points:
    0
    #6
    ok thanks... will look into it
     
    bobby9101, Dec 11, 2006 IP
  7. drewbe121212

    drewbe121212 Well-Known Member

    Messages:
    733
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    125
    #7
    Yeah. Actually, just check out "JOINS", as their are many types. In this particular situation, I personally would use a LEFT JOIN, but an INNER JOIN would work just as well.
     
    drewbe121212, Dec 12, 2006 IP
    Jat likes this.