Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / ColdFusion / Getting Started / January 2008



Tip: Looking for answers? Try searching our database.

SQL question

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Hydrowizard - 30 Jan 2008 18:50 GMT
Hi all I am trying to do "like" to find a string in a where clause. It works
fine like this:
<cfquery name="qrysales" datasource="mydatasource" >
SELECT Referer, visit, id
FROM clientinfo
where Referer like '%google%'
</cfquery>

but now I am trying to do it for various and also set them as aliases:

<cfquery name="qrysales" datasource="mydatasource" >
SELECT Referer, visit, id
FROM clientinfo
where Referer like '%google%' as google
and Referer like '%msn%' as msn
</cfquery>

but I get this error:
You have an error in your SQL syntax; check the manual that corresponds to
your MySQL server version for the right syntax to use near 'as google and
Referer like '%msn%' as msn' at line 3

I am doing this right? What is the syntax? TIA
paross1 - 30 Jan 2008 18:56 GMT
Why on earth would you try to create an alias for a parameter in a WHERE
clause? What are you expecting to do with this alias?

Also, [b]where Referer like '%google%' and Referer like '%msn%'[/b] would only
return rows where Referer contained both google and msn in the same row.

Phil
Hydrowizard - 30 Jan 2008 19:41 GMT
Hi Phil,
   I am going to use the info for a cfchart but that is by the by really.

What I am going to do to output all the values from the query. <cfoutput
query="qrysales">#qrysales.referer#</cfoutput>

What I was doing before was this:
<cfoutput query="qrysales">
<cfif find("google.it", qrysales.referer)>
      Google Italy
<cfelseif find("google", qrysales.referer)>
      Google
<cfelseif find("msn", qrysales.referer) or find("live.com", qrysales.referer)>
      MSN Live
<cfelseif find("yahoo", qrysales.referer)>
      Yahoo
<cfelseif qrysales.referer does not contain "google" or qrysales.referer does
not contain "yahoo"
 or qrysales.referer does not contain "msn">
      Other search engine
      </cfif>

but this isn?t needed now. What I want to do is set in the sql these
conditions to variables/values. I always understood that it is best to do
everything in the sql. Please advise as this is confusing.
Hydrowizard - 30 Jan 2008 20:32 GMT
I?m going to look at query of a query again I think this is the only way to do this
paross1 - 30 Jan 2008 20:38 GMT
[Q]I always understood that it is best to do everything in the sql.[/Q] Not
necessarily. Also, nothing is ever "returned" from the WHERE clause of a query,
only the SELECT. If you have a small set of posible values that will be
matching in your query, you may be able to use a CASE statement in your SELECT,
but that can get real nasty, especially as you add values to your Referer
table, since that would require you to modify your query, which is NOT a
desireable circumstance.

Something like this, perhaps.

SELECT
 CASE
    WHEN referer LIKE '%google.it%' THEN 'Google Italy'
    WHEN referer LIKE '%google'% THEN 'Google'
    WHEN referer LIKE '%msn%' OR referer LIKE '%live.com%' THEN 'MSN Live'
    WHEN referer LIKE '%yahoo%' THEN 'Yahoo'
    ELSE 'Other search engine'
 END AS new_referer
FROM    clientinfo
WHERE    whatever

Phil
Hydrowizard - 30 Jan 2008 20:56 GMT
Thanks that is some pretty hefty sql. So either I get into some sql trouble or
look at query of query? I am going to use the case solution for the time being.
Excuse my ignorance but what is the other way so I can study how to do it?
paross1 - 30 Jan 2008 21:06 GMT
[Q]what is the other way...[/Q]Like you were trying to do before, with if/then/else login in ColdFusion.
Hydrowizard - 30 Jan 2008 21:28 GMT
Other way: OK so then I can set those values to variables like this:

<cfoutput query="qrysales">
<cfif find("google.it", qrysales.referer)>
Google Italy <cfset googleit=qrysales.referer>
<cfelseif find("google", qrysales.referer)>
Google <cfset google=qrysales.referer>
<cfelseif find("msn", qrysales.referer) or find("live.com", qrysales.referer)>
MSN Live <cfset msn=qrysales.referer>
<cfelseif find("yahoo", qrysales.referer)>
Yahoo <cfset yahoo=qrysales.referer>
<cfelseif qrysales.referer does not contain "google" or qrysales.referer does
not contain "yahoo"
or qrysales.referer does not contain "msn">
Other search engine <cfset otherse=qrysales.referer>
</cfif>
</cfoutput>

How do I get a "record count" of the newly set variables? len(googleit) give
me the chars in each record not the number of records?
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.