質問

I'm running the following SQL queries and am not getting a desired output:

 <cfquery datasource = "XX.XX.X.XX" name="master">
    SELECT count(Timedetail) as Occurances, date_format(Timedetail,'%m-%d-%Y') 
    FROM   MyDatabase
    WHERE  EVENTS = "FIRST" GROUP BY Timedetail ;
 </cfquery> 
<cfquery dbtype="query" name="detail">
    SELECT  *
    FROM    master 
    WHERE  Timedetail >= <cfqueryparam value="#form.startdate#" cfsqltype="cf_sql_date"> 
    AND    Timedetail <  <cfqueryparam value="#dateAdd('d', 1,form.enddate)#" cfsqltype="cf_sql_date">;
</cfquery> 

The relevant database columns are:

  • TimeDetail: Holds all date and time related values
  • Events: Contains values like, FIRST, SECOND,THIRD etc. I have mentioned FIRST here for convenience and clarity.

As far as the startdate and enddate parameters are concerned, I have set them as follows:

 <cfparam name="form.startdate" default="#dateformat(now()-5, 'mm/dd/yyyy')#">
 <cfparam name="form.enddate" default="#dateformat(now()-1, 'mm/dd/yyyy')#">
 <cfparam name="form.selectdate" default="#dateformat(now(), 'mm/dd/yyyy')#">

So, my master query is displaying the following results:

  OCCURANCES  TIMEDETAIL
1   15712   06-06-2013
2   7533    06-07-2013
3   20899   06-10-2013
4   24075   06-11-2013
5   24219   06-12-2013
6   21485   06-13-2013
7   22661   06-14-2013
8   20010   06-15-2013
9   18032   06-16-2013
10  27588   06-17-2013
11  25861   06-18-2013
12  21106   06-19-2013
13  22281   06-20-2013
14  21736   06-21-2013
15  20060   06-22-2013
16  18384   06-23-2013
17  24233   06-24-2013
18  39901   06-25-2013
19  31132   06-26-2013
20  41744   06-27-2013
21  38926   06-28-2013
22  34910   06-29-2013
23  25682   06-30-2013
24  48400   07-01-2013
25  42847   07-02-2013
26  30014   07-03-2013
27  21047   07-04-2013
28  29982   07-05-2013
29  25056   07-06-2013
30  13733   07-07-2013
31  35753   07-08-2013
32  20966   07-09-2013
33  41713   07-10-2013
34  30976   07-11-2013 

And, I'm wondering why my "detail" query is displaying nothing although I have specified startdate parameter as 2013-06-12 and enddate parameter as 2013-07-12 which is visible in the resultset as mentioned below. It should display the occurances and timedetail for the date range I have specified.

RESULTSET   
query
    OCCURANCES  TIMEDETAIL
CACHED  false
EXECUTIONTIME   0
SQL     SELECT * FROM master WHERE Timedetail >= ? AND Timedetail <?;
SQLPARAMETERS   
array
1   {ts '2013-06-12 00:00:00'}
2   {ts '2013-07-12 00:00:00'}  
役に立ちましたか?

解決 2

Well I figured it out on my own. The MySQL date_format() function returns a string, not a datetime object, so when running my query of query in ColdFusion I used the CF_SQL_VARCHAR datatype instead of CF_SQL_DATE and everything worked fine.


(Update from comments)

The answer above turned out to be wrong. When I used CF_SQL_VARCHAR I was getting unexpected results, in fact incomplete results. When I applied the STR_TO_DATE function, it worked fine.

他のヒント

In memory queries (QoQ's) can be tricky when it comes to data types. They are far less savy about implicit data type conversions than a database would be. By using MySQL's date_format function, you are actually converting the datetime values into strings. So when you run your QoQ, CF may actually be performing a string comparison, which would yield very different results than a date comparison. Could explain why you are getting the wrong results.

Try changing your database query to return a datetime value instead of a string:

SELECT 
   COUNT(Timedetail) as Occurances
   , STR_TO_DATE( DATE_FORMAT(Timedetail,'%m-%d-%Y'), '%m-%d-%Y') AS Timedetail
FROM   ....
WHERE  ...

Update:

Another option is to CAST the value as a DATE in your QoQ. That would force the QoQ to perform a date comparison, instead of a string comparison:

WHERE  CAST(Timedetail AS DATE) >= <cfqueryparam value="#form.startdate#" 
                                          cfsqltype="cf_sql_date"> 
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top