尝试在使用cfif和执行查询的查询中比较行值和表单值

时间:2013-05-13 18:52:25

标签: mysql sql coldfusion

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

 <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_nameform.search_bar之类的内容时,我收到了错误消息。可能是因为ColdFusion无法识别变量,因为从技术上讲,它还没有在ColdFusion的范围内创建,仍然是等待执行的mysql代码。

现在我知道还有其他方法可以做到这一点(比如让html选择下拉并让用户选择他们真正想要搜索的内容。但是我很想知道我在这里做的是否可能用另一种我没有想到的解决方案。

错误消息的图片: Error Message

错误本身

  

元素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>

您会认为这样可行吗?它没有按我预期的方式工作。比方说,例如,我在DB中有10个条目,8个国家为'Merica',2个国家为'Nippon'。用户在该字段中输入“Merica”并期望“Merica”结果。相反,结果是所有条目,包括'Nippon'。

这里是截图:

Oh Nippon

搜索结果的文字:

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的结果。同样,如果我搜索“测试主题”,我只希望测试对象出现。不是测试对象和bob因为bob在他的记录中的某个地方进行了测试。

编辑:我为此创建一个自包含环境的最佳方法是创建一个包含所有必需文件的git,用于支持的功能。我的托管服务提供商没有coldfusion支持,我无法在那里安装,因为我通过终端缺少root访问权限。天啊,我甚至不能在它上面安装coldfusion。但无论如何,这里是git的链接。 sql文件可供您直接导入到数据库模式,以创建我正在为此应用程序使用的模式。

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

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

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分: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.