1

我目前正在尝试找到一种方法来忽略该字段为空白的搜索中的任何结果。简单的方法是使用 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,但这似乎根本没有区别。有人能指出我正确的方向吗?谢谢。

4

2 回答 2

1

这最终是一个微不足道的错误,但是值得向确实发现类似问题的任何人指出,如果您根据现有变量添加额外的语句,请检查它们在查询的所有级别中都不是必需的。

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 结果的部分。

于 2013-05-07T13:21:15.560 回答
0

@variables尝试在 sql server 中使用动态方法:

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 +'%')
于 2013-05-08T23:44:45.257 回答