?
Solved

Joining two fields together for searching.

Posted on 2011-03-21
9
Medium Priority
?
291 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
Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

 
LVL 11

Accepted Solution

by:
Brijesh Chauhan earned 668 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 668 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 664 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

Major Serverless Shift

Comparison of major players like AWS, Microsoft Azure, IBM Bluemix, and Google Cloud Platform

Question has a verified solution.

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

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

752 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