Skip to main content
Known Participant
January 7, 2010
Answered

Query Help Needed! 2 tables

  • January 7, 2010
  • 2 replies
  • 644 views

Using Coldfusion 8 and MySQL.  I have 2 tables one called Products (100 items) and another table called Clearance (5 items).

I am displaying all the 100 products and need to insert the discounted_price of the 5 items from the Clearance table into the query below.

(*note- the discounted_price field only exists in the Clearance table).

Also, how can the returned list below mark the checkboxes of the 5 items from the Clearance table.

(*note-productid is used in both tables.I guess you could call it the foreign key between the tables.)

I'm a novice and unsure how to write the queries.  Any help would be appreciated.  Thanks in advance.

<cfoutput query="getProducts">

<tr<cfif currentrow mod 2> class="odd"</cfif>>

    <td><div align="left"><input type="checkbox" name="promo" value="##"></td>

     <td>(#getProducts.modelnumber#) #getProducts.modelnumber#</td>

     <td>#getProducts.modeldescription#</td>

     <td>#getProducts.category#</td>

     <td>#getProducts.subcategory#</td>

     <td><input type="text" name="discounted_price" value=""></td>

</tr>

</cfoutput>

    This topic has been closed for replies.
    Correct answer

    <cfquery name="getProducts" ds="#urdatasource#">
    select p.modelnumber, p.modeldescription, p.category, p.subcategory, C.discounted_price
    from Products P left join Clearance C on C.productid = P.productid
    </cfquery>


    <cfoutput query="getProducts">

    <tr<cfif currentrow mod 2> class="odd"</cfif>>

        <td><div align="left"><input type="checkbox" name="promo" <cfif getProducts.discounted_price neq "">checked</cfif> ></td>

         <td>(#getProducts.modelnumber#) #getProducts.modelnumber#</td>

         <td>#getProducts.modeldescription#</td>

         <td>#getProducts.category#</td>

         <td>#getProducts.subcategory#</td>

         <td><input type="text" name="discounted_price" value=""></td>

    </tr>

    </cfoutput>

    Hope this helps....

    2 replies

    Correct answer
    January 8, 2010

    <cfquery name="getProducts" ds="#urdatasource#">
    select p.modelnumber, p.modeldescription, p.category, p.subcategory, C.discounted_price
    from Products P left join Clearance C on C.productid = P.productid
    </cfquery>


    <cfoutput query="getProducts">

    <tr<cfif currentrow mod 2> class="odd"</cfif>>

        <td><div align="left"><input type="checkbox" name="promo" <cfif getProducts.discounted_price neq "">checked</cfif> ></td>

         <td>(#getProducts.modelnumber#) #getProducts.modelnumber#</td>

         <td>#getProducts.modeldescription#</td>

         <td>#getProducts.category#</td>

         <td>#getProducts.subcategory#</td>

         <td><input type="text" name="discounted_price" value=""></td>

    </tr>

    </cfoutput>

    Hope this helps....

    Known Participant
    January 8, 2010

    thanks!

    Known Participant
    January 8, 2010

    OK. I figured out that I need to do a LEFT OUTER JOIN.

    Now.  How do I get the 5 appropriate checkboxes marked?  Plus, how do I do an UPDATE and INSERT on the entire query results?

    Inspiring
    January 8, 2010

    write your query something like this:

    select regular.item_id, ifnull(discount.price, regular.price), discount.something_else

    from regular left join discount on regular.item_id = discount.item_id

    Then, when you output your query to generate the checkboxes, check them for the rows where discount.something_else is not null.

    Known Participant
    January 8, 2010

    thanks!