sql joins

Discussion in 'MySQL' started by cashboy, May 28, 2006.

  1. #1
    I have 2 different mysql tables.. and I want to get data from both of them through only one query.. I know it can be done using joins.. but dont know how...

    can anyone help me?
     
    cashboy, May 28, 2006 IP
  2. themole

    themole Peon

    Messages:
    82
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #2
    themole, May 28, 2006 IP
  3. goldensea80

    goldensea80 Well-Known Member

    Messages:
    422
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    128
    #3
    Two table must have some thing to join, for example, one table is categories have the key name cat_id, other is posts which also have cat_id to specify which category it belongs to.
    Now the query is something like this

    "
     
    goldensea80, May 29, 2006 IP
  4. kLdd15

    kLdd15 Peon

    Messages:
    16
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    remember that there are multiple types of joins. When there is a normal join, it only returns data if there are matching rows. When using left join foo; then it will show the other table and include the rows with no matching results in the 2nd db.
     
    kLdd15, Jun 6, 2006 IP
  5. dddougal

    dddougal Well-Known Member

    Messages:
    676
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    108
    #5
    It depends what you want the join to do, give us a clue. If you just want results from both, one after the other then id use 'UNION' to combine results from both tables.

    Like themole said, a 2 second search on google will reveal all!
     
    dddougal, Jun 9, 2006 IP