ColdFusion Query of Query Oddity With Table-Prefixed Columns And ORDER BY

Posted August 3, 2006 at 9:14 AM

Tags: ColdFusion

NOTE: I was asked to remove someone's name from this blog entry. I am waaaaay too lazy to remake these images (just as I am too lazy to make up names). If you would like to see the original post please contact me and if I have some spare time, I can remake.

I am running into this really weird ColdFusion query of queries "bug" when I prefix columns with the table name; one of the columns is getting selected twice for no apparent reason. To demonstrate (as I can easily produce this bug), let's build a simple query:

 Launch code in new window » Download code as text file »

  • <!--- Create a base query of data. --->
  • <cfset qGirl = QueryNew( "id, first_name, last_name" ) />
  •  
  • <!--- Add data to that query. --->
  • <cfset QueryAddRow( qGirl, 3 ) />
  • <cfset qGirl[ "id" ][ 1 ] = 1 />
  • <cfset qGirl[ "first_name" ][ 1 ] = "Molly" />
  • <cfset qGirl[ "last_name" ][ 1 ] = "Clarke" />
  •  
  • <cfset qGirl[ "id" ][ 2 ] = 2 />
  • <cfset qGirl[ "first_name" ][ 2 ] = "Sarah" />
  • <cfset qGirl[ "last_name" ][ 2 ] = "Vivenzio" />
  •  
  • <cfset qGirl[ "id" ][ 3 ] = 3 />
  • <cfset qGirl[ "first_name" ][ 3 ] = "xxxxx" />
  • <cfset qGirl[ "last_name" ][ 3 ] = "yyyyy" />

If you dump this query out, it looks as you would expect:


 
 
 

 
CFDump - qGirl  
 
 
 

Now, let's perform a query of queries on the table and order by the first and last names:

 Launch code in new window » Download code as text file »

  • <cfquery name="qAllGirls" dbtype="query">
  • SELECT
  • id,
  • first_name,
  • last_name
  • FROM
  • qGirl
  • ORDER BY
  • first_name ASC,
  • last_name ASC
  • </cfquery>

If you dump this query out, again, exactly as you would expect it:


 
 
 

 
qallgirls gif  
 
 
 

Now, let's run that same exact query again, but this time, you prefix the column names with the tablet that they are coming from (which is exactly what you would have to do in a JOIN situation):

 Launch code in new window » Download code as text file »

  • <cfquery name="qAllGirls2" dbtype="query">
  • SELECT
  • qGirl.id,
  • qGirl.first_name,
  • qGirl.last_name
  • FROM
  • qGirl
  • ORDER BY
  • qGirl.first_name ASC,
  • qGirl.last_name ASC
  • </cfquery>

Dumping this query out, you will clearly see that the last column in the ORDER BY clause is getting selected twice. And, it always seems to be the last column; if I were to switch the first and last name columns, then the first name would be selected twice.


 
 
 

 
qallgirls2 gif  
 
 
 

Now, in the above example, I am only using two ORDER BY rules. If you put in more than two, the results get even more crazy. In the following, I am going to order by ID as well:

 Launch code in new window » Download code as text file »

  • <cfquery name="qAllGirls3" dbtype="query">
  • SELECT
  • qGirl.id,
  • qGirl.first_name,
  • qGirl.last_name
  • FROM
  • qGirl
  • ORDER BY
  • <!--- Added the ID sort here. --->
  • qGirl.id ASC,
  • qGirl.first_name ASC,
  • qGirl.last_name ASC
  • </cfquery>

WHAT!?! Now, BOTH first name and last name are getting selected twice! My SELECT statement has not changed at all.


 
 
 

 
qallgirls3 gif  
 
 
 

Ok, for one final little experiment, I am trying only order by ID, but order by ID several times:

 Launch code in new window » Download code as text file »

  • <cfquery name="qAllGirls4" dbtype="query">
  • SELECT
  • qGirl.id,
  • qGirl.first_name,
  • qGirl.last_name
  • FROM
  • qGirl
  • ORDER BY
  • qGirl.id ASC,
  • qGirl.id ASC,
  • qGirl.id ASC
  • </cfquery>

Dumping this out, the results are just too odd to explain:


 
 
 

 
qallgirls4 gif  
 
 
 

When I look in the ColdFusion Live Docs, I am not seeing anything about this bug in the ORDER BY documentation.

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Permalink  |  Other Searches  |  Print Page



Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

Aug 7, 2006 at 3:21 PM // reply »
18 Comments

Send this to Damon Cooper at Adobe.


May 18, 2007 at 10:55 AM // reply »
1 Comments

Came across the same bug independently. Thanks for the nice documentation!


Post Comment  |  Ask Ben

Recent Blog Comments
Nov 22, 2009 at 1:45 AM
Streaming Text Using ColdFusion's CFContent Tag And The Variable Attribute
The reason you would want to do this is to stream. Ack json/xml files to ria clients I used thus technique before because putting json in response stream causes debugging info to come thru As well a ... read »
Nov 21, 2009 at 6:47 PM
Hal Helms - Real World Object Oriented Development, Sarasota - Day Five
@charlie griefer, Thank you.. ... read »
Nov 21, 2009 at 5:15 PM
Using ColdFusion Structures To Remove Duplicate List Values
@Jose Galdamez, Oh heh yeah I didn't paste the whole code. I should have defined the vars -- my bad. It's fixed thou. Thanks. ... read »
Nov 21, 2009 at 4:49 PM
Styling The ColdFusion 8 WriteToBrowser CFImage Output
Great work yet again Ben! Whilst I didn't use this whole code, I copied some of your regex code for a similar problem with the lack of an alt attribute and unescaped ampersands in CFIMAGE for Railo 3 ... read »
Nov 21, 2009 at 1:13 PM
My First ColdFusion Builder Extension - Encrypting And Decrypting CFM / CFC Files
@Ben, Because I am pedantic, I just want to make sure that everyone knows there is absolutely no encryption going on. There is only encoding and obfuscation. The cfencode tool only obfuscates your C ... read »
Nov 21, 2009 at 12:28 PM
Using ColdFusion Structures To Remove Duplicate List Values
@Jody I can't seem to get your code sample to work. If you are still having problems, try this code out and see if it gets you what you wanted. <!--- Comma delimited list with various duplicates ... read »
Nov 21, 2009 at 11:03 AM
Groovy Operator Overloading Does Not Work In The ColdFusion Context
Hi Ben, Thanks for this informative post. Now I am reading ur old posts too ... read »
Nov 21, 2009 at 10:56 AM
HostMySite.com Has The Best ColdFusion Hosting
@Mehul, Yes very nice people, however several downtimes per day which was not acceptable. Hence we had to move out. I am glad you are having good luck with them so far. ... read »