Solved

Joining two fields together for searching.

Posted on 2011-03-21
9
285 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
Comment Utility
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
Comment Utility
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
Comment Utility
<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
 
LVL 11

Accepted Solution

by:
Brijesh Chauhan earned 167 total points
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 23

Assisted Solution

by:OP_Zaharin
OP_Zaharin earned 167 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
<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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
Recently while working on a project I got a very annoying cfdocument has no body error message. I had never seen this error before. So I checked the code. The code was pretty simple; it was Just showing me the cfdocumnt tag and inside that tag a …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

771 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now