Solved

Joining two fields together for searching.

Posted on 2011-03-21
9
290 Views
Last Modified: 2012-05-11
Hello,

I have a last name and first name field in my table but want to be able to submit a form with someone either entering a first name or last name or both last name and first name.

My code is below but it's not quite working how I need it yet.

Thanks,

Brian
<cfquery name="qSearch" datasource="#request.dsn#" username="#request.dsnUsername#" password="#request.dsnPassword#">
    SELECT *
    FROM site_users t1
    WHERE last_name +' '+ first_name = '%#FORM.field_search#%'
</cfquery>

Open in new window

0
Comment
Question by:brihol44
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +3
9 Comments
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 35186888
try like this

<cfquery name="qSearch" datasource="#request.dsn#" username="#request.dsnUsername#" password="#request.dsnPassword#">
    SELECT *
    FROM site_users t1
    WHERE last_name +' '+ first_name = '%#FORM.field_search#%'
 OR last_name  = '%#FORM.field_search#%' Or first_name = '%#FORM.field_search#%'
</cfquery>
0
 

Author Comment

by:brihol44
ID: 35186962
Hello,

Seems to be pulling all of my names no matter what. All of the first names and last names are unique. If I enter just the first name. It pulls all of the records. If I enter the last name same thing or both the last name and first name.

Thank you for the help...

Brian
0
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 35187601
<cfquery name="qSearch" datasource="#request.dsn#" username="#request.dsnUsername#" password="#request.dsnPassword#">
    SELECT *
    FROM site_users t1
    WHERE last_name +' '+ first_name = '#FORM.field_search#%'
 OR last_name  = '#FORM.field_search#' Or first_name = '#FORM.field_search#'
</cfquery>
0
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 
LVL 11

Accepted Solution

by:
Brijesh Chauhan earned 167 total points
ID: 35187672
What is your default storage engine ? You can find it in my.ini file, is it INNODB or MYISAM ?

If you are using MYISAM then you can take advantage of MySQL full text searching.
0
 
LVL 23

Assisted Solution

by:OP_Zaharin
OP_Zaharin earned 167 total points
ID: 35187683
do you want them to enter exact name or they can key-in 'BRI' and list all name like 'BRI? try this:

- if exact name:
<cfquery name="qSearch" datasource="#request.dsn#" username="#request.dsnUsername#" password="#request.dsnPassword#">
    SELECT *
    FROM site_users t1
    WHERE 
    last_name + ' ' + first_name = '#FORM.field_search#' OR
    last_name = '#FORM.field_search#' OR
    first_name = '#FORM.field_search#' 
</cfquery>

Open in new window


- if search for name like 'something':
<cfquery name="qSearch" datasource="#request.dsn#" username="#request.dsnUsername#" password="#request.dsnPassword#">
    SELECT *
    FROM site_users t1
    WHERE 
    last_name + ' ' + first_name LIKE '%#FORM.field_search#%' OR
    last_name LIKE '%#FORM.field_search#%' OR
    first_name LIKE '%#FORM.field_search#%' 
</cfquery>

Open in new window

0
 
LVL 11

Expert Comment

by:Brijesh Chauhan
ID: 35187709
If you are using MYISAM, then you just have to do

<cfquery name="qSearch" datasource="#request.dsn#" username="#request.dsnUsername#" password="#request.dsnPassword#">
    SELECT *
    FROM site_users t1
    WHERE 
    MATCH (last_name,first_name)
    AGAINST ('#FORM.field_search#')
</cfquery>

Open in new window


the table column should be fullindex, this performs much faster.
0
 
LVL 39

Expert Comment

by:gdemaria
ID: 35189857
Your code needs to use LIKE instead of = in order to use the wildcards...

   WHERE last_name +' '+ first_name LIKE '%#FORM.field_search#%'

However, this will not match if the person enters the name in the wrong order.  You are testing SMITH BOB, if someone enters BOB SMITH, there will be no match.

If you want to extend your search, you can check each word seperately as show in the code below.  However, this will return BOB JONES, JOHN SMITH and BOB SMITH.  That may be helpful to your app, it may not.



<cfquery name="qSearch" datasource="#request.dsn#" username="#request.dsnUsername#" password="#request.dsnPassword#">
    SELECT *
    FROM site_users t1
    WHERE 1=1 
   <cfif len(trim(form.field_search))> <!---- make sure some text was entered ---->
    and (
   <cfset isFirst = true>
   <cfloop index="aWord" list="#form.field_search#" delimiters=", ">
    <cfif isFirst><cfset isFirst = false><cfelse>OR</cfif>
     first_name + ' ' + last_name LIKE '%aWord%'
   </cfloop>
    )
   <cfelse> <!--- if no search term entered, return nothing, remove this to return ALL records ---->
    and 1=2
   </cfif>
</cfquery>

Open in new window

0
 
LVL 2

Expert Comment

by:ravi_bachwala
ID: 35195333
<cfquery name="qSearch" datasource="#request.dsn#" username="#request.dsnUsername#" password="#request.dsnPassword#">
    SELECT *
    FROM site_users t1
    WHERE last_name +' '+ first_name LIKE '%#FORM.field_search#%'
    OR first_name  LIKE '%FORM.field_search%'
    OR last_name LIKE '%FORM.field_search%'
</cfquery>
0
 
LVL 39

Assisted Solution

by:gdemaria
gdemaria earned 166 total points
ID: 35195400
Wow, that looks familiar,  but maybe I am missing something here...


Isn't it true that anything that matches one of these ...

  first_name  LIKE '%FORM.field_search%'
  last_name LIKE '%FORM.field_search%'

will also match this...

   last_name +' '+ first_name LIKE '%#FORM.field_search#%'



So using this...

    WHERE last_name +' '+ first_name LIKE '%#FORM.field_search#%'
    OR first_name  LIKE '%FORM.field_search%'
    OR last_name LIKE '%FORM.field_search%'

Is the same as filtering with this...

    WHERE last_name +' '+ first_name LIKE '%#FORM.field_search#%'


?

0

Featured Post

Webinar: MongoDB® Index Types

Join Percona’s Senior Technical Services Engineer, Adamo Tonete as he presents “MongoDB Index Types, How, When and Where Should They be Used?” on Wednesday, July 12, 2017 at 11:00 am PDT / 2:00 pm EDT (UTC-7).

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question