0

我知道,标题听起来令人困惑,但我真的想不出另一种说法。无论如何,这就是我的意思

 <cfquery name="search_all" datasource="ContactData">
   SELECT DISTINCT 
          people.first_name,
          people.last_name,
          state_lkp.state,
          zip_lkp.zip,
          number_lkp.phone_number,
          email_lkp.email,
          country_lkp.country,
          address_lkp.address,
          city_lkp.city,
          count(distinct email_lkp.email_id) as mail_count,
          count(distinct number_lkp.phone_number_id) as number_count,
          people.people_id
   FROM   people 
             Left Join state_lkp On state_lkp.people_id = people.people_id 
             Left Join zip_lkp On zip_lkp.people_id = people.people_id
             Left Join number_lkp On number_lkp.people_id = people.people_id 
             Left Join email_lkp On email_lkp.people_id = people.people_id 
             Left Join country_lkp On country_lkp.people_id = people.people_id 
             Left Join city_lkp On city_lkp.people_id = people.people_id 
             Left Join address_lkp On address_lkp.people_id = people.people_id
  WHERE 
<cfif LCase(people.last_name) eq LCase(form.search_bar)>
    people.last_name LIKE LTrim(RTrim(<cfqueryparam value="%#form.search_bar#%" cfsqltype="cf_sql_varchar" maxlength="500">))
<cfelseif  LCase(people.first_name) eq LCase(form.search_bar)>
    people.first_name LIKE LTrim(RTrim(<cfqueryparam value="%#form.search_bar#%" cfsqltype="cf_sql_varchar" maxlength="500">))
<cfelseif  LCase(address_lkp.address) eq LCase(form.search_bar)>
    address_lkp.address LIKE LTrim(RTrim(<cfqueryparam value="%#form.search_bar#%" cfsqltype="cf_sql_varchar" maxlength="500">))
<cfelseif LCase(coutnry_lkp.country) eq LCase(form.search_bar)>
    country_lkp.country LIKE LTrim(RTrim(<cfqueryparam value="%#form.search_bar#%" cfsqltype="cf_sql_varchar" maxlength="500">))
<cfelseif LCase(city_lkp.city) eq LCase(form.search_bar)>
    city_lkp.city LIKE LTrim(RTrim(<cfqueryparam value="%#form.search_bar#%" cfsqltype="cf_sql_varchar" maxlength="500">))
<cfelseif LCase(state_lkp.state) eq LCase(form.search_bar)>
    state_lkp.state LIKE LTrim(RTrim(<cfqueryparam value="%#form.search_bar#%" cfsqltype="cf_sql_varchar" maxlength="500">))
<cfelseif LCase(zip_lkp.zip) eq LCase(form.search_bar)>
    zip_lkp.zip LIKE LTrim(RTrim(<cfqueryparam value="%#form.search_bar#%" cfsqltype="cf_sql_varchar" maxlength="500">))
<cfelseif LCase(email_lkp.email) eq LCase(form.search_bar)>
   email_lkp.email LIKE LTrim(RTrim(<cfqueryparam value="%#form.search_bar#%" cfsqltype="cf_sql_varchar" maxlength="500">))
<cfelseif LCase(number_lkp.phone_number) eq LCase(form.search_bar)>
    number_lkp.phone_number LIKE LTrim(RTrim(<cfqueryparam value="%#form.search_bar#%" cfsqltype="cf_sql_varchar" maxlength="500">))
</cfif>
GROUP BY people.people_id desc;
</cfquery>

看到那里的 cfif/cfelseif 语句了吗?当我尝试比较类似的东西people.last_name时,form.search_bar我得到了一个错误。可能是因为 ColdFusion 无法识别该变量,因为从技术上讲,它还没有在 ColdFusion 的范围内创建,并且仍然是等待执行的 mysql 代码。

现在我知道还有其他方法可以做到这一点(比如制作一个 html 选择下拉菜单并让用户选择他们实际想要搜索的内容。但是我很想知道我在这里所做的事情是否可以通过另一种解决方案来实现我没有想到。

错误信息的图片: 错误信息

错误本身

元素 LAST_NAME 在 PEOPLE 中未定义。

如果我需要澄清一些事情,请告诉我。感谢您花时间看这个问题。


编辑:注意我最初的查询是这样的:

<cfquery name="search_all" datasource="ContactData">
Select distinct
  people.first_name,
  people.last_name,
  state_lkp.state,
  zip_lkp.zip,
  number_lkp.phone_number,
  email_lkp.email,
  country_lkp.country,
  address_lkp.address,
  city_lkp.city,
count(distinct email_lkp.email_id) as mail_count,
count(distinct number_lkp.phone_number_id) as number_count,
people.people_id
From
  people 
Left Join state_lkp On state_lkp.people_id = people.people_id 
Left Join zip_lkp On zip_lkp.people_id = people.people_id
Left Join number_lkp On number_lkp.people_id = people.people_id 
Left Join email_lkp On email_lkp.people_id = people.people_id 
Left Join country_lkp On country_lkp.people_id = people.people_id 
Left Join city_lkp On city_lkp.people_id = people.people_id 
Left Join address_lkp On address_lkp.people_id = people.people_id
WHERE 

people.last_name LIKE LTrim(RTrim(<cfqueryparam value="%#form.search_bar#%" cfsqltype="cf_sql_varchar" maxlength="500">))
OR people.first_name LIKE LTrim(RTrim(<cfqueryparam value="%#form.search_bar#%" cfsqltype="cf_sql_varchar" maxlength="500">))
OR address_lkp.address LIKE LTrim(RTrim(<cfqueryparam value="%#form.search_bar#%" cfsqltype="cf_sql_varchar" maxlength="500">))
OR country_lkp.country LIKE LTrim(RTrim(<cfqueryparam value="%#form.search_bar#%" cfsqltype="cf_sql_varchar" maxlength="500">))
OR city_lkp.city LIKE LTrim(RTrim(<cfqueryparam value="%#form.search_bar#%" cfsqltype="cf_sql_varchar" maxlength="500">))
OR state_lkp.state LIKE LTrim(RTrim(<cfqueryparam value="%#form.search_bar#%" cfsqltype="cf_sql_varchar" maxlength="500">))
OR zip_lkp.zip LIKE LTrim(RTrim(<cfqueryparam value="%#form.search_bar#%" cfsqltype="cf_sql_varchar" maxlength="500">))
OR email_lkp.email LIKE LTrim(RTrim(<cfqueryparam value="%#form.search_bar#%" cfsqltype="cf_sql_varchar" maxlength="500">))
OR number_lkp.phone_number LIKE LTrim(RTrim(<cfqueryparam value="%#form.search_bar#%" cfsqltype="cf_sql_varchar" maxlength="500">))

group by people.people_id desc;
</cfquery>

你会认为这行得通吗?它没有按我预期的方式工作。比如说,我在数据库中有 10 个条目,8 个有一个国家“Merica”,2 个国家有“Nippon”。用户在字段中输入“Merica”并期望“Merica”结果。相反,结果是所有条目,包括“日本”条目。

在这里,一个屏幕截图:

哦日本

搜索结果的文本:

First Name Last Name Address    City    Zip     Country State   Mail Count Number Count
Test    Subject 123 Imperfect rd Huge   12546   USA     TX  1           1
Yelp    Pley    616 Symmetry Rd Kinikinik 15051 USA     Co  1           1
Son     Goku    560 Nowhere ave None     None  Nippon   None    1           1
My  Addias  8998 Beat St    Breakin 12478   USA     NY  1           1
Gotta   Yolo    123 Only Once st. Miami 04211   USA     FL  1           1

归根结底是什么。如果我搜索 Merica 结果。我只想要 Merica 结果的完全匹配,而不是包含 merica 的结果,而不是在国家/地区。同样,如果我搜索“测试主题”,我只希望出现测试主题的人。不测试主题和鲍勃,因为鲍勃在他的记录中某处进行了测试。

编辑:为此创建一个自包含环境的最好方法是创建一个 git,其中包含支持功能的所有必要文件。我的托管服务提供商没有 Coldfusion 支持,我无法在那里安装它,因为我缺乏通过终端的 root 访问权限。见鬼,我什至不能 sudo install coldfusion 。但无论如何,这里是 git 的链接。您可以将 sql 文件导入到 db 模式中,以创建与我用于此应用程序的相同的模式。

https://github.com/VinceOmega/Contact-Manager

感谢所有试图提供帮助的人。

4

2 回答 2

0

我认为这可能是您正在寻找的:

<cfquery name="search_all">
SELECT DISTINCT
    people.first_name,
    people.last_name,
    state_lkp.state,
    zip_lkp.zip,
    number_lkp.phone_number,
    email_lkp.email,
    country_lkp.country,
    address_lkp.address,
    city_lkp.city,
    count(distinct email_lkp.email_id) as mail_count,
    count(distinct number_lkp.phone_number_id) as number_count,
    people.people_id
FROM
    people 
    LEFT JOIN state_lkp ON state_lkp.people_id = people.people_id 
    LEFT JOIN zip_lkp ON zip_lkp.people_id = people.people_id
    LEFT JOIN number_lkp ON number_lkp.people_id = people.people_id 
    LEFT JOIN email_lkp ON email_lkp.people_id = people.people_id 
    LEFT JOIN country_lkp ON country_lkp.people_id = people.people_id 
    LEFT JOIN city_lkp ON city_lkp.people_id = people.people_id 
    LEFT JOIN address_lkp ON address_lkp.people_id = people.people_id
WHERE
    people.last_name LIKE <cfqueryparam value="%#trim( lCase(form.search_bar) )#%" cfsqltype="cf_sql_varchar" maxlength="500"> OR
    people.first_name LIKE <cfqueryparam value="%#trim( lCase(form.search_bar) )#%" cfsqltype="cf_sql_varchar" maxlength="500"> OR
    people.first_name LIKE <cfqueryparam value="%#trim( lCase(form.search_bar) )#%" cfsqltype="cf_sql_varchar" maxlength="500"> OR
    address_lkp.address LIKE <cfqueryparam value="%#trim( lCase(form.search_bar) )#%" cfsqltype="cf_sql_varchar" maxlength="500">
    -- more filters here
GROUP BY
    people.people_id DESC
</cfquery>
于 2013-05-13T19:04:16.613 回答
0

您不能在 CFIF 语句中使用诸如 people.last_name 之类的 SQL 引用,如下所示:

<cfif LCase(people.last_name) eq LCase(form.search_bar)>
    people.last_name LIKE LTrim(RTrim(<cfqueryparam value="%#form.search_bar#%" cfsqltype="cf_sql_varchar" maxlength="500">))

如果您尝试创建搜索多个字段的单个搜索,只需将一系列 OR 语句串在一起:

WHERE
    people.first_name like <cfqueryparam value="%#trim(form.search_bar)#%" cfsqltype="cf_sql_varchar" maxlength="500">
    OR people.last_name like <cfqueryparam value="%#trim(form.search_bar)#%" cfsqltype="cf_sql_varchar" maxlength="500">
etc. etc.
于 2013-05-13T19:05:31.313 回答