Question

I am trying to retrieve and store ID's for each item retrieved from my table, so I can use these ID's later. I tried nesting the queries, but this didn't, work. Here is my first query:

<CFQUERY datasource="MyDSN" name="MAIN2"> SELECT * from order_items where orderID= #orderID#</CFQUERY>

Now, if I output this query it displays, 1 and 117 which are the two ID's I need.

My next query is:

<CFQUERY datasource="MyDSN" name="MAIN3">select c.catalogueID,
c.product_name,
c.product_price,
c.description,
p.productID

from products p
join product_catalogue c on c.catalogueid = p.catalogueid
where p.productid = "#productID#"</CFQUERY>

But it is telling me that productID is not defined, it is obviously empty. I am just getting started using ColdFusion, so I am not sure the best way to store the values I need so I use them again. I also need to loop the second query to run for each ID 1 and 117, so twice.

Any suggestions on how to accomplish this would be greatly appreciated.

Thanks

Was it helpful?

Solution

My basic rule is that if I find myself using queries to create other queries or looping over a query to execute other queries; it is time to consider combining the queries.

I'm not sure what field you are using in the MAIN2 query to feed the MAIN3 query. So, I put in "productID" in the query below. You may have to change it to fit your field name.

<CFQUERY datasource="MyDSN" name="MAIN3">select c.catalogueID,
c.product_name,
c.product_price,
c.description,
p.productID

from products p
join product_catalogue c on c.catalogueid = p.catalogueid
where p.productid IN (SELECT DISTINCT productID from order_items where orderID= <cfqueryparam value="#orderID#" cfsqltype="CF_SQL_INTEGER">)
</CFQUERY>

You could also change this query to utilize a "join" to connect [order_items] to the query.

Lastly, you should use the <cfqueryparam> tag for the where clauses; this helps protect your query from sql injection attacks.

OTHER TIPS

Whenever I'm caching data for use later, I tend to ask myself how I'll be using that data, and whether it belongs in another data type rather than query.

For instance, if I'm wanting a bunch of data that I'm likely to access via ID, I can create a structure where the key is the ID, and the data is another structure of a dataset. Then I'll save this structure in application scope and only refresh it when it needs to be. This is zippy fast and so much easier to grab with rather than querying for it every time. This is especially useful when the query that creates the original data set is kind of a resource hog with lots of joins, sub-queries, magical cross-db stored procedures, but the datasets returns are actually fairly small.

So creating your products structure would look something like this:

<CFQUERY datasource="MyDSN" name="MAIN3">
  SELECT 
    c.catalogueID,
    c.product_name,
    c.product_price,
    c.description,
    p.productID
  FROM products p
  JOIN product_catalogue c 
  ON c.catalogueid = p.catalogueid
  WHERE p.productid = <cfqueryparam value="#ProductID#" cfsqltype="cf_sql_integer">
</CFQUERY>

<cfset products = structNew() />
<cfset item = structNew() />
<cfloop query="MAIN3">
   <cfif NOT structKeyExists(products, productID)>
      <cfset item = structNew() />
      <cfset item.catalogueID = catalogueID />
      <cfset item.product_name = product_name />
      <cfset item.product_price = product_price />
      <cfset item.description = description />
      <cfset products[productID] = structCopy(item) />
   </cfif>
</cfloop>

<cfset application.products = structCopy(products) />
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top