我目前正试图找到一种方法来省略字段为空的搜索结果。简单的方法是使用<cfif len(example) neq 0>#example#</cfif>
,但这意味着我的查询不必要地提取结果。
<cfquery datasource="#datasource#" name="search">
<cfif URL.for eq "names">
Select artist_id, artist_name
From artists
Where approved = 1
And active = 1
<cfif StructKeyExists("URL.search")>
<cfif URL.find eq "contains">
And name LIKE <cfqueryparam cfsqltype="cf_sql_varchar" value="%#URL.search#%">
<cfelseif URL.find eq "matches">
And name = <cfqueryparam cfsqltype="cf_sql_varchar" value="#URL.search#">
</cfif>
</cfif>
Order By name Asc
<cfelseif URL.for eq "locations">
<cfif URL.sort eq "town-az" or URL.sort eq "town-za">
Select Distinct town
<cfelseif URL.sort eq "county-az" or URL.sort eq "county-za">
Select Distinct county
<cfelseif URL.sort eq "country-az" or URL.sort eq "country-za">
Select Distinct country
</cfif>
From artists
Where approved = 1
And active = 1
<cfif StructKeyExists("URL.search")>
<cfif URL.find eq "contains">
<cfif URL.sort eq "town-az">
And town LIKE <cfqueryparam cfsqltype="cf_sql_varchar" value="%#URL.search#%">
Order By town Asc
<cfelseif URL.sort eq "town-za">
And town LIKE <cfqueryparam cfsqltype="cf_sql_varchar" value="%#URL.search#%">
Order By town Desc
<cfelseif URL.sort eq "county-az">
And county IS NOT NULL
And county LIKE <cfqueryparam cfsqltype="cf_sql_varchar" value="%#URL.search#%">
Order By county Asc
<cfelseif URL.sort eq "county-za">
And county IS NOT NULL
And county LIKE <cfqueryparam cfsqltype="cf_sql_varchar" value="%#URL.search#%">
Order By county Desc
<cfelseif URL.sort eq "country-az">
And country LIKE <cfqueryparam cfsqltype="cf_sql_varchar" value="%#URL.search#%">
Order By country Asc
<cfelseif URL.sort eq "country-za">
And country LIKE <cfqueryparam cfsqltype="cf_sql_varchar" value="%#URL.search#%">
Order By country Desc
</cfif>
<cfelseif URL.find eq "matches">
<cfif URL.sort eq "town-az">
And town = <cfqueryparam cfsqltype="cf_sql_varchar" value="#URL.search#">
Order By town Asc
<cfelseif URL.sort eq "town-za">
And town = <cfqueryparam cfsqltype="cf_sql_varchar" value="#URL.search#">
Order By town Desc
<cfelseif URL.sort eq "county-az">
And county IS NOT NULL
And county = <cfqueryparam cfsqltype="cf_sql_varchar" value="#URL.search#">
Order By county Asc
<cfelseif URL.sort eq "county-za">
And county IS NOT NULL
And county = <cfqueryparam cfsqltype="cf_sql_varchar" value="#URL.search#">
Order By county Desc
<cfelseif URL.sort eq "country-az">
And country = <cfqueryparam cfsqltype="cf_sql_varchar" value="#URL.search#">
Order By country Asc
<cfelseif URL.sort eq "country-za">
And country = <cfqueryparam cfsqltype="cf_sql_varchar" value="#URL.search#">
Order By country Desc
</cfif>
</cfif>
</cfif>
</cfif>
</cfquery>
注册时town
和country
字段是必填字段,因此这些字段没有问题,但我没有将县/州字段设为必填字段,因此如果值为NULL,则值为NULL用户不进入县或州。如上所述,我可以简单地使用len()
方法仅显示具有字符的结果,但这意味着我的查询将拉出NULL结果,而不需要将其拉出。我已经包含了AND county IS NOT NULL
,但这似乎没有任何区别。有人能够指出我正确的方向吗?感谢。
答案 0 :(得分:1)
最终这是一个微不足道的错误,但是如果你根据现有的变量添加额外的语句,那么值得向任何找到类似问题的人指出,检查它们在查询的所有级别都不是必需的。< / p>
SELECT DISTINCT county
FROM artists
WHERE active = 1
AND approved = 1
<cfif URL.sort eq "county-az" or URL.sort eq "county-za">
AND county IS NOT NULL
</cfif>
以上是我需要在查询核心内更改的内容,请注意我已将该部分包括在内以排除所有NULL结果 BEFORE 检查是否存在搜索字词。
答案 1 :(得分:0)
尝试在sql server中使用@variables
的动态方法:
declare @county varchar(50), @countylike varchar(50)
添加一些逻辑来设置ColdFusion中存在的变量:
<cfif ....>
</cfif>
然后在where子句中添加这样的东西:
and (@county is null or county = @county)
and (@countylike is null or county like '%'+ @countylike +'%')