I am stumped! A MySQL Join and CF QUERY Question

Discussion in 'Programming' started by Coldfusionstudent, May 14, 2009.

  1. #1
    Dear Kind Forum members,

    I am stumped with trying to make the table joins and query as seen in the attached image.

    I am at a total lost at what to do. I tried making left joins for all the tables, but when I try to write the query output, the rows seem to double or triple. For example, if I try to use the cfoutput to display the toppings for B_ID 1, it would show "Mayo, Ketchup, Mustard" x 3 becuase the Ingredient table had 3 records of having the B_ID that is being used to join all the tables. Or if, the B_ID only had 1 Topping row in the Topping table, but the same B_ID had 4 Ingredients in the Ingredient table, when I display the cfoutput for the Topping for that B_ID, it will show the same answer repeated 4 times becuase that B_ID had 4 Ingredients in the Ingerdient table.

    I am not sure if I am making my question clear.

    I have attached s diagram to try to make my question clearer. Thank you very very much for anyone who can show me the light. I will be very grateful.
     

    Attached Files:

    Coldfusionstudent, May 14, 2009 IP
  2. Coldfusionstudent

    Coldfusionstudent Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I may not be too clear, and so will try to describe the issue here:

    I have 3 tables.

    Table 1 named "T_Questions"

    Question_ID(Pk)     Questions             Burger_ID(FK)
    1                   How Much?              1
    2                   Has Seasoning?         1
    3                   Type of Bread?         1
    4                   Where is Spock?        3
    5                   What Bread?            3
    6                   What Sauce?            3
    HTML:
    Table 2 named "T_Comments"

    Comments_ID(Pk)     Comments             Burger_ID(FK)
    1                   Too Fatty              1
    2                   Too Small              2
    3                   Perfect Taste!         1
    4                   Too Big                1
    5                   Too Slim!              2
    6                   Too Spacey!            3
    HTML:
    Table 3 named "T_BurgerOrder"

    Burger_ID(Pk)     Burger_Name             OrderName
    1                 Mighty Joe Burger        Jerry
    2                 Slim Jim Burger          Florence
    3                 Startreck Burger         Jerry
    
    HTML:
    This is how I joined the tables:

    <cfquery datasource="#DS#" name="MyBurgers">

    SELECT *
    FROM t_BurgerOrder
    LEFT JOIN t_Questions
    ON T_BurgerOrder.Burger_ID = t_Questions.Burger_ID
    LEFT JOIN T_Comments
    ON T_BurgerOrder.Burger_ID = T_Comments.Burger_ID
    Where OrderName = "Jerry"

    </CFQuery>

    This is the simple code on the page to test my output format.

    <cfoutput query="MyBurgers" Group="Burger_ID">

    #Burger_ID#, <cfoutput>#Comments_ID#</cfoutput> <br/>

    </cfoutput>


    This is what is displayed after the cf code above is run.

    1, (1)(3)(4)(1)(3)(4)(1)(3)(4)
    3, (6)(6)(6)

    As you can see, The output above shows that the Burger with Burger_ID '1' has 3 comments, but the answer repeats 3 times as there are also 3 records with that same foreign key 'Burger_ID' in the T_Questions table.

    Similarly, even though Burger with Burger_ID 3 only has 1 comment in the comment table, it spits out the answer 3 times as there are 3 Questions with the same Burger_ID in the T_Question table.

    How would i solve this issue so that it only shows each answer once like below?

    1, (1)(3)(4)
    3, (6)

    Thanks so much again for your kind help. I have really been feeling down about this issue :( and as i am new, I really do not know how to go about solving this. Thank you again.
     
    Coldfusionstudent, May 15, 2009 IP