[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Joining two fields together for searching.

Posted on 2011-03-21
9
Medium Priority
?
294 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
  • 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
NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Creating and Managing Databases with phpMyAdmin in cPanel.
Recursive SQL is one of the most fascinating and powerful and yet dangerous feature offered in many modern databases today using a Common Table Expression (CTE) first introduced in the ANSI SQL 99 standard. The first implementations of CTE began ap…
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 Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses
Course of the Month20 days, 8 hours left to enroll

867 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