我目前正在尝试找到一种方法来忽略该字段为空白的搜索中的任何结果。简单的方法是使用 a<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。如上所述,我可以简单地使用该len()
方法只显示包含字符的结果,但这意味着我的查询正在提取它不需要提取的 NULL 结果。我已经包括在内AND county IS NOT NULL
,但这似乎根本没有区别。有人能指出我正确的方向吗?谢谢。