I am selecting a full name from two columns in mysql table but the input takes only one order to return the information.
firstname | lastname
Amaj | Ato
With this data I want to select the full name (Amaj Ato or Ato Amaj) from one text box when the user enters Amaj Ato or vice versa (Ato Amaj).
This is what I have tried so far
`SELECT * FROM `table_name` WHERE concat_ws(' ', 'firstname', 'lastname') like '%$fullname%'; `
This is able to select the firstname and lastname in only one order. For instance if the user enters Amaj Ato the query gives the full name but when the user enters the firstname after the lastname ( like Ato Amaj), the query returns empty. I want the user to get the full name in any order he enters the names. Thanks for helping.
You need to "tell" the query what you are looking for, it doesn't magically know that names could be switched. To do this, you can simply add an OR specifying the other valid case:
SELECT * FROM `table_name`
WHERE concat_ws(' ', 'firstname', 'lastname') like '%$fullname%'
OR concat_ws(' ', 'lastname', 'firstname') like '%$fullname%'
Related
i have this query :
SELECT
CONCAT(CT.FIRSTNAME,' ', CT.LASTNAME) AS NAME,
T.TYPE_OF_VISITOR_NAME AS TYPE_OF_VISITOR,
I.INDUSTRY_NAME AS INDUSTRY
FROM
COMPANY CY, CONTACTS CT, INDUSTRY I, TYPE_OF_VISITOR T
WHERE
CY.INDUSTRY_ID = I.INDUSTRY_ID
AND CY.COMPANY_ID = CT.COMPANY_ID
AND CT.TYPE_OF_VISITOR = T.TYPE_OF_VISITOR_ID
AND '$searchType' LIKE '%$searchString%'
where $searchType is a value from a combo box which contains 2 values :
NAME (this is not recognized in the database as long as i use CONCAT(CT.FIRSTNAME,' ', CT.LASTNAME) the problem is that when user picked COMPANY_NAME i will have wrong syntax)
COMPANY_NAME
and $searchString is an input box for search.
in the query above. when user searches for anything under COMPANY_NAME type it works. the problem is that i want to have a search option also for name(first name and last name). unfortunately the columns in my database is FIRSTNAME and LASTNAME. which i concatenated in the select statement.
Question :
What is the right way to achieve the search for FIRSTNAME and LASTNAME without affecting the result when user wants to search for COMPANY_NAME
As much as possible i want to do this using one query only. Any help would be much appreciated. Thanks
Instead of NAME, the combo box value should be
CONCAT(`CT`.`FIRSTNAME`,' ', `CT`.`LASTNAME`)
combo box values :
1. CONCAT(`CT`.`FIRSTNAME`,' ', `CT`.`LASTNAME`)
2. `COMPANY_NAME`
I have my website where I want to add search functionality for the user. My search criteria is First name and last Name with two different column in mysql databse.
eg: If user has entered xxx (First name) yyy (last Name) in search box then the result is like:
If exact match with this two string then only one result has to come
from database.
If doesn't match then simply contain in from database has to come. (eg
xx is present then it has to come)
I've used the following query:
Select * from mytable where FirstName like '%".$fname."%' or LastName like '%".$lname."%' order by DtLastModified desc
it is giving me all the result which matches this string.
eg:
xxx yyy
xx yy
x y
x
y
Can any one please help me on this.
this may work, replace your php variables for $fname $lname
SELECT * FROM mytable
WHERE CONCAT(firstname, '', lastname) LIKE '%".$fname.$lname."%'
OR (firstname LIKE '%".$fname ."%' OR lastname LIKE '%". $lname ."%')
For : if exact match with this two string then only one result has to come from database
replace your query from 'or' to 'and' lik below
Select * from mytable where FirstName like '%".$fname."%' and LastName like '%".$lname."%' order by DtLastModified desc
I have a search system which helps you filter people list
My Table (People)
id | name | surname | email
There is a textbox which you can enter the name, surname or email so the list is filtered as you type.
It finds the records if I write only name or only surname. But if I enter name and surname it does show nothing.
I have tried Mysql's CONCAT function. But it did not work.
My query:
SELECT *
FROM People
WHERE name LIKE :p1
OR surname LIKE :p2
OR email LIKE :p3
I have tried this
SELECT *
FROM People
WHERE name LIKE :p1
OR surname LIKE :p2
OR email LIKE :p3
OR CONCAT(name, char(32), surname) LIKE :p4
and this
SELECT id,
name,
surname,
CONCAT(name, char(32), surname) AS fullname
FROM People
WHERE name LIKE :p1
OR surname LIKE :p2
OR email LIKE :p3
OR fullname LIKE :p4
How can I obtain the system that can find results when name + [space] + surname is entered?
You can split the keywords and search for each individual keyword (or expression) in your columns (name, surname and email).
OK, I have a DB table that's called players and each player has a forename and surname. Then I have a PHP Ajax search thing that I call to search for players. For example... in the input box, someone types James and there's a row in the table with forename and surname James and Smith respetively.
I do this $check = mysql_query("SELECT * FROMplayersWHEREsurnameLIKE '%$name%' ORforenameLIKE '%$name%' LIMIT 0, 10") or die(mysql_error());
And it returns at least 10 with either forename or surname like the keyword James. However, if I type James Smith, despite it being in the table, I get zero results.
How do I fix this?
Are you using InnoDB or MyISAM? If your using MyISAM, you can create a single field which holds the combined name and then search it using a full text index. So lets imagine you add a new field called combined_names you would search it like this
SELECT * FROM table WHERE match(combined_names) against('John Smith');
This would find any row with either John or Smith in, you can change it to match only those rows with both parts you would add plusses like so:
SELECT * FROM table WHERE match(combined_names) against('+John +Smith');
Here is the documentation on the MySQL site where you can find out more:
http://dev.mysql.com/doc/refman/5.0/en/fulltext-search.html
SELECT * FROM players WHERE CONCAT(forename, ' ', surname) = '$name' OR forename LIKE '%$name%' OR surname LIKE '%$name%'
split the name up on spaces so the query runs twice (if there is one space)
the query will run for both names
$nameBits = explode($name," ");
run the query for each piece of $nameBits
surname LIKE '%$nameBits[$i]%'
I am writing a search query which makes a search based on first name and last name.
The query is like this:
select fname,lname
from users
where fname like fname like '%$name%' or lname like '%$name%';
if the user enters first name and last name in the search box,I have exploded it with a 'space' and the query goes like this
select fname,lname
from users
where fname like fname like '%$f_name%' or lname like '%$l_name%';
In the second query if I enter John Thomas,it shows me all the records with John or Thomas in first or last name,but the actual search result i.e John Thomas(exact match) is somewhere below in the results.(If I have 100 results its on the 60th position).
How can I modify the query to achive this or do I have to handle it programatically? i.e Check the result array and match the check box value for its presence and display it fist.
Try:
SELECT fname, lname FROM users
WHERE (fname = '$f_name' AND lname = '$l_name')
OR (fname LIKE '%$f_name%') OR (lname LIKE '%$l_name%');
And if you need to order then ORDER BY lname, fname in the end of the query, depending on what you need to order by of course.
ADD in query
ORDER BY `fname`
You can testing mysql sorting
More information here ==> http://dev.mysql.com/doc/refman/5.0/en/sorting-rows.html