SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Guru
    Join Date
    Sep 2004
    Location
    Provo, UT
    Posts
    858
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Searching on full name with firstname lastname columns

    My users are stored in the database with a firstname lastname. For this project I need to be able to search on their full name like, "David Jones".

    I've used concat a little bit to produce a full name in the query results, but is the same thing possible when searching?

    Thanks!
    Convert your dollars into silver coins. www.convert2silver.com

  2. #2
    SitePoint Wizard donboe's Avatar
    Join Date
    Jun 2010
    Location
    Netherlands
    Posts
    2,106
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code MySQL:
    SELECT  
        concat( firstName, ' ', lastName ) AS fullName 
    FROM yourTable
    WHERE fullName LIKE ......
    “Good artists copy, great artists steal” (Pablo Picasso - 1934)

  3. #3
    SitePoint Guru
    Join Date
    Sep 2004
    Location
    Provo, UT
    Posts
    858
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    #1054 - Unknown column 'fullName' in 'where clause'
    Convert your dollars into silver coins. www.convert2silver.com

  4. #4
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,263
    Mentioned
    60 Post(s)
    Tagged
    3 Thread(s)
    yeah, you can't use a SELECT clause column alias in the WHERE clause

    you have to do this --
    Code:
    SELECT CONCAT( firstName, ' ', lastName ) 
                  AS fullName 
      FROM yourTable
     WHERE CONCAT( firstName, ' ', lastName ) LIKE ...
    alternatively, this --
    Code:
    SELECT fullName 
      FROM ( SELECT CONCAT( firstName, ' ', lastName ) 
                         AS fullName 
               FROM yourTable ) AS d
     WHERE fullName LIKE ...
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  5. #5
    SitePoint Wizard donboe's Avatar
    Join Date
    Jun 2010
    Location
    Netherlands
    Posts
    2,106
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by busboy View Post
    #1054 - Unknown column 'fullName' in 'where clause'
    Oops my wrong! Thanks rudy
    “Good artists copy, great artists steal” (Pablo Picasso - 1934)


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •