Searching first and last name for a full name

Discussion in 'MySQL' started by danramosd, Apr 14, 2010.

  1. #1
    I have database that contains first and last names in separate columns. I have a live search that currently searches first OR last names, but not a full name. My current query is:
    
    SELECT id, fname, lname, sport,state, school FROM coach WHERE lname LIKE '%$query%' OR fname LIKE '%$query%' LIMIT 5
    
    Code (markup):
    How do go about searching for a full name? For instance if someone types "Bob Smith" right now nothing would be returned because there is no first or last name of "Bob Smith", but there is a person with the first name Bob and the last name Smith
     
    danramosd, Apr 14, 2010 IP
  2. weaverIT

    weaverIT Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Try this ,
    SELECT id, fname, lname, sport,state, school FROM coach WHERE CONCAT(fname, "", lname) LIKE '%$query%' LIMIT 5
     
    weaverIT, Apr 14, 2010 IP
  3. georgiivanov

    georgiivanov Member

    Messages:
    62
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    25
    #3
    Create a FULL TEXT index on both columns - first and last name. It will work far better than LIKE statement.

    FULL TEXT index can be created only for MyISAM tables
     
    georgiivanov, Apr 15, 2010 IP