Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Coldfusion Query ORDER BY syntax

New Here ,
Oct 28, 2009 Oct 28, 2009

Hi,

I have products stored in different categories and I want to run a query that will display the products either by product name or by product price depending on the category ID.  I have it working with just one category being sorted by product name (as below)  but how do I specify for multiple categories e.g. IF #URL.cat# EQ "10" OR "11" OR "5" OR "14" ???

<cfif #URL.cat# EQ "10">
<cfquery name="catalogue" datasource="#request.SiteDSN#" username="#request.DSNUser#" password="#request.DSNPasswd#">
SELECT *
FROM products
WHERE prod_cat_id = <cfqueryPARAM value = "#URL.cat#"
   CFSQLType = "CF_SQL_INTEGER"> ORDER BY prod_name
</cfquery>

<cfelse>
<cfquery name="catalogue" datasource="#request.SiteDSN#" username="#request.DSNUser#" password="#request.DSNPasswd#">
SELECT *
FROM products
WHERE prod_cat_id = <cfqueryPARAM value = "#URL.cat#"
   CFSQLType = "CF_SQL_INTEGER"> ORDER BY prod_price
</cfquery>
</cfif>

Thanks in advance!

595
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 28, 2009 Oct 28, 2009

I would do one query with the IF statement in the order by:

<cfquery name="catalogue" datasource="#request.SiteDSN#" username="#request.DSNUser#" password="#request.DSNPasswd#">
SELECT *
FROM products
WHERE prod_cat_id = <cfqueryPARAM value =
"#URL.cat#"
   CFSQLType =
"CF_SQL_INTEGER">
ORDER BY <cfif ListFind("5,10,11,14",URL.cat)>prod_name<cfelse>prod_price</cfif>
</cfquery>
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 29, 2009 Oct 29, 2009
LATEST

My example goes for economy, writing the query just once.

<cfswitch expression="#URL.cat#">

<!--- categories 5, 10 or 12 --->
<cfcase value="5,10,12">
        <cfset orderByClause="prod_name">
    </cfcase>

<!--- categories 11 or 14 --->
    <cfcase value="11,14">
        <cfset orderByClause="prod_price">
    </cfcase>

<!--- all categories other than 5,10,11,12,14 --->
    <cfdefaultcase>
        <cfset orderByClause="prod_name">
    </cfdefaultcase>
</cfswitch>

<cfquery name="catalogue" datasource="#request.SiteDSN#" username="#request.DSNUser#" password="#request.DSNPasswd#">
SELECT *
FROM products
WHERE prod_cat_id = <cfqueryPARAM value = "#URL.cat#" CFSQLType = "CF_SQL_INTEGER">

ORDER BY #orderByClause#
</cfquery>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources