can i get some help on JOINS please....

Discussion in 'Databases' started by mz906, Jan 13, 2009.

  1. #1
    k, so normally i know its bad to come out and ask for straight code...but i am (sort of)...

    i've got a old school query:

    
    select 
        mc.id, 
        mc.created, 
        title, 
        body, 
        mu.user_name, 
        mcat.cat_name
    from
        mini_content as mc, 
        mini_post as p, 
        mini_user as mu, 
        mini_category_relationship as mcr, 
        mini_category as mcat
    where 
        mc.id = p.content_id
    and 
        mu.id = mc.user_id
    and 
        mcr.post_id = mc.id
    and 
        mcr.cat_id = mcat.cat_id
    
    Code (markup):
    as you can see there's alot of relationships going on, now before you panic, the above code prints this out:
    
    id    created       title      body                  user_name      cat_name 
    --+-------------+-----+-------------------+--------------+----------+
    3   2009-01-13   bbl    be back later         Amanda Barry  art
    4   2009-01-13   ttyn   talk to you never   Jane Smith      art 
    2 rows in set (0.00 sec)
    Code (markup):
    so my question is...how do i re-write my query using JOINS? (ok...i know this is a very basic 'blog' style table and of course i could of just used one table and been done with it, but i've got another thread going bout that :)

    this is kinda my exercise on how to better use/understand joins
     
    mz906, Jan 13, 2009 IP
  2. mwasif

    mwasif Active Member

    Messages:
    816
    Likes Received:
    23
    Best Answers:
    1
    Trophy Points:
    70
    #2
    select 
        mc.id, 
        mc.created, 
        title, 
        body, 
        mu.user_name, 
        mcat.cat_name
    from
       mini_content as mc 
       INNER JOIN mini_post as p ON mc.id = p.content_id 
       INNER JOIN mini_user as mu ON mu.id = mc.user_id
       INNER JOIN mini_category_relationship as mcr ON mcr.post_id = mc.id
       INNER JOIN mini_category as mcat ON mcr.cat_id = mcat.cat_id
    Code (markup):
     
    mwasif, Jan 14, 2009 IP
  3. mz906

    mz906 Peon

    Messages:
    101
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    thanks for the reply, i was getting lost cause sometimes i see them with '( )' and other times they don't have them, whats the difference?
     
    mz906, Jan 14, 2009 IP
  4. CreativeClans

    CreativeClans Peon

    Messages:
    128
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    See what with '()' ?
     
    CreativeClans, Jan 15, 2009 IP
  5. mapAffiliation

    mapAffiliation Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    "old school query".... hm ... it is not "old scholl" it has another meaning and different results.
     
    mapAffiliation, Jan 16, 2009 IP
  6. mz906

    mz906 Peon

    Messages:
    101
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    i picked that up from another, post, thats what someone else referred to it as
     
    mz906, Jan 16, 2009 IP
  7. webprone

    webprone Well-Known Member

    Messages:
    163
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    133
    #7
    you basically first need to determine which of the tables used in the joins is your main table from which you want to fetch data.And then use suitable outer joins on other tables(Informix DB).

    The above querry can be better and more efficiently written as:

    select 
        mc.id, 
        mc.created, 
        title, 
        body, 
        mu.user_name, 
        mcat.cat_name
    from
       mini_content mc 
       outer mini_post p  
       outer mini_user mu  
       outer mini_category_relationship mcr  
       outer mini_category mcat 
    where mc.id = p.content_id 
        and mu.id = mc.user_id
        and mcr.post_id = mc.id
        and mcr.cat_id = mcat.cat_id
    Code (markup):
     
    webprone, Jan 16, 2009 IP