Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. How to Search for names in MySQL

How to Search for names in MySQL

Scheduled Pinned Locked Moved Database
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jassim Rahma
    wrote on last edited by
    #1

    Hi, I have mySQL with first_name, middle_name and last_name fields. How can I pass a string from my php like param_name so when the user pass single name or multiple name it will split and search for the name in every fields.. For example if the user search for param_name = ""John Mathew Clark" it will do: first_name or middle_name or last_name LIKE "%John%" first_name or middle_name or last_name LIKE "%Mathew%" first_name or middle_name or last_name LIKE "%Clark%" Thanks, [Jassim](https://www.softnames.com)

    Technology News @ www.JassimRahma.com

    M 1 Reply Last reply
    0
    • J Jassim Rahma

      Hi, I have mySQL with first_name, middle_name and last_name fields. How can I pass a string from my php like param_name so when the user pass single name or multiple name it will split and search for the name in every fields.. For example if the user search for param_name = ""John Mathew Clark" it will do: first_name or middle_name or last_name LIKE "%John%" first_name or middle_name or last_name LIKE "%Mathew%" first_name or middle_name or last_name LIKE "%Clark%" Thanks, [Jassim](https://www.softnames.com)

      Technology News @ www.JassimRahma.com

      M Offline
      M Offline
      Mycroft Holmes
      wrote on last edited by
      #2

      I don't know about MySQL but in SQL Server I have done the following in the past. Build a function that takes a string and splits it on a delimiter (space) and return a table. In your query left outer join the function, passing in your name field, on your name field with Name = or LIKE '%' + itemFromFunction + '%' Make the result set DISTINCT. Not sure if = or like is needed in the LOJ. This is an old SQL Server split function I dug up.

      ALTER FUNCTION [dbo].[fn_Split]
      (@List varchar(8000), @Delimiter char(1))

      RETURNS @Results table
      (Item varchar(8000),ID int Identity(1,1))

      AS

      begin
      declare @IndexStart int
      declare @IndexEnd int
      declare @Length int
      declare @Word varchar(8000)

      set @IndexStart = 1
      set @IndexEnd = 0

      set @Length = len(@List)
      If @Delimiter = '' Set @Delimiter = ','

      --Get rid of any tabs or returns
      Set @List = Replace(@List,char(9),'')
      Set @List = Replace(@List,char(10),'')
      Set @List = Replace(@List,char(13),'')

      while @IndexStart <= @Length
      begin
      set @IndexEnd = charindex(@Delimiter, @List, @IndexStart)

      If @Delimiter = char(32) 
      	set @IndexEnd = charindex(Space(1), @List, @IndexStart)
      
      if @IndexEnd = 0
      	set @IndexEnd = @Length + 1
      
      set @Word = substring(@List, @IndexStart, @IndexEnd - @IndexStart)
      set @IndexStart = @IndexEnd + 1
      
      INSERT INTO @Results(Item)
      SELECT @Word
      

      end

      return
      end

      Never underestimate the power of human stupidity RAH

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups