Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Query Weirdness

Status
Not open for further replies.

Sarky78

Programmer
Oct 19, 2000
878
GB
I have got a simple query that I have used for a while, to get the newest record from a table. I have added a where clause to the query so that I only get the newest record for the category that i want. The problem that I am having is that the query is returning a record when one does not exist. I think that the where clause is being ignored in the first query (getMaxQuestion), and returning incorrect information.

The query looks like this:

<cfquery datasource=&quot;test&quot; ame=&quot;getMaxQuestion&quot;>
SELECT Max(CompetitionID) as maxComp
FROM tblCompetition
WHERE Hotel = #variables.hotel#
</cfquery>

<cfquery datasource=&quot;test&quot; name=&quot;getQuestion&quot;>
SELECT *
FROM tblCompetition
WHERE CompetitionID = #getMaxQuestion.maxComp#
</cfquery>

Is this the case, can anyone think of a way of getting around this problem ??
 
-> this is a sql question and as such it would be better in the SQL forum
-> which is the query returning an answer even where there's no answer ?
-> add the maxrow=1 in the cfquery tag of the 1st query, as you expect only one row !
 
CFHub I am not getting the newest ID from the DB. I am using the same table for three sister sites for sections that are the same. There will be three administrators that can only add to their site. These use three seperate Hotel ID's, that allow me to identify the competition for that specific hotel. The newest competition may not be the competition for that hotel, so using the that method is not going to work.

What I have tried is:

SELECT MAX(CompetitionID) as max
FROM tblComps
WHERE Hotel = #variables.hotelID#

This is returning information for the wrong hotel. The #variables.hotelID# is defined in each of the sites app.cfm's. what it appears to be doing is ignoring the where statement, and just returning the newest(max) entry.

Any ideas why the where clause would b ignored ?
 
yes ! USE CFOUTPUT !!!!!!!!!!!! else you're looking for the &quot;#variable.hotelID#&quot; string NOT its value !!!!!!
 
ITS IN A CFQUERY YOU DON'T NEED A CFOUTPUT !!!!!

This is the debug code from the page, showing the query with the variable that has been picked up from the app.cfm !!!!

BEFORE COMPILATION

SELECT MAX(CompetitionID) as max
FROM tblComps
WHERE Hotel = #variables.hotelID#

AFTER COMPILATION

SELECT Max(CompetitionID) as maxComp
FROM tblCompetition
WHERE Hotel = 2
 
oh ! shame on me it's been to long i haven't coded a cfm app !!!
and you have an hotel which id is 2 in the db, and you don't get any value ?????? go to sql forum then, i'm sure people there are better than us coldfusion coders to find out what you're mising then !!
 
Been over there and at the moment no response. There is no entry in the database for hotel 2, but that is a possibility so has to be tested for. At the moment it is retuirning a record, when there is nothing in the database for that hotel ! (weird) and when i do an output on that there is nothing in the value to show so it is just drving me up the wall!!!

Will see if i can bother the SQL lot then for a while !!

Cheers

 
ok i've got a tricky idea, but it's not elegant at all and i guess a pure sql guy would get totally mad reading this (it's just because i don't know how max works ...) and i'm not even sure it'll be working better !!!

SELECT nvl(MAX(CompetitionID), &quot;no_value&quot;) as max
FROM tblComps
WHERE Hotel = #variables.hotelID#

and then just add
<cfif #getMaxQuestion.max# neq &quot;no_value&quot;>
<cfquery datasource=&quot;test&quot; name=&quot;getQuestion&quot;>
....
<cfelse>
i'm sorry this hotel doesn't even exists !!
</cfif>

let me know ....
hey, are those real hotels ? could we win a night in one of those ;]]]] !???



 
thinking along the same lines here. I have just done this:

<CFQUERY NAME=&quot;getMaxQuestion&quot; DATASOURCE=&quot;test&quot;>
SELECT Max(CompetitionID) as maxComp
FROM tblCompetition
WHERE Hotel = #variables.hotel#
</CFQUERY>
<cfif #Trim(getMaxQuestion.maxComp)# NEQ &quot;&quot;>
<cfquery datasource=&quot;test&quot; name=&quot;getQuestion&quot;>
SELECT *
FROM tblCompetition
WHERE CompetitionID = #getMaxQuestion.maxComp#
</cfquery>
</CFIF>

and that seems to work fine, when there is a record and when there isn't a record !
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top