Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Searching full name or first or last name in MySQL database with first and last name in separate columns

Tags:

I'm working with an existing database that has first name and last name seperated in the database. I need to create a function that will take one search input and return results. say my database has a structure like....

nameFirst nameLast Joe       Smith Joe       Jones Joe       Brown 

How could I, using MySql, take a search input that is say 'Joe Smith' and just get his row? But if I put just 'Joe' in the search field, return them all? Will I need to explode the string with a space? thanks!

like image 675
G.Thompson Avatar asked Apr 06 '12 17:04

G.Thompson


People also ask

How do I find first name and last name in SQL?

SELECT. left(NAME, charindex(' ', NAME) - 1) AS 'FirstName', REVERSE(SUBSTRING(REVERSE(NAME), 1, CHARINDEX(' ', REVERSE(NAME)) - 1)) AS 'LastName'

How do I find the last name in MySQL?

Try: WHERE SUBSTRING_INDEX(fullname, " ", -1) = 'lastname' (dev.mysql.com/doc/refman/5.5/en/…) Or consider storing the first and last names in separate fields.

How do I search for a specific database in MySQL?

In the Search phrase box, enter the search string and if it is not chosen, from the Database drop-down box, select a database to search in. In the search grid, choose tables and views of interest or leave them all checked. To narrow down the MySQL search data scope, select the table, views, numeric, text type, and date columns checkboxes.

How to display columns name first name and last name in MySQL?

MySQL query to display columns name first name, last name as full name in a single column? MySQL query to display columns name first name, last name as full name in a single column? For this, use CONCAT () method in MySQL.

What is full text search in MySQL?

MySQL full-text search Full-text search is a MySQL search technique to search for data in a database. Please note that not all engines support the full-text search feature. Starting from MySQL version 5.6 or higher, the MyISAM and InnoDB storage engines support a full-text search.

How to concatenate first name and last name to full name?

Following is the query to concatenate first name and last name to full name − mysql> select concat(FirstName,' ',LastName) AS FullName from DemoTable; This will produce the following output −


1 Answers

SELECT *  FROM table WHERE CONCAT( nameFirst,  ' ', nameLast ) LIKE  '%Joe%'   

Be sure to sanitize any user submitted parameters such as "Joe"

like image 161
csi Avatar answered Sep 22 '22 19:09

csi