Reputation: 19
I need help making a search mysql query in multiple fields and i also want it to return the field(s) where the keywords match with the field value:
example:
$query = mysql_query("SELECT Name, MATCHFIELD FROM `employes` WHERE `Name` LIKE '%$keyword%' || `Prenom` LIKE '%$keyword%' || `Telephone` LIKE '%$keyword%' || `Telephone2` LIKE '%$keyword%'");
while($row = mysql_fetch_array($query)){
echo $row['name'].':'.$row['MATCHFIELD'];
}
MATCHFIELD is not a field in my database, it has to be the name of the field where the "LIKE" found something
this is supposed to return something like this: "John bishop 332521455"
the number "332521455" could be in any field.
This code is a part of a autosuggest script so the query has to return only rows where a field matchs with a field value.
Upvotes: 1
Views: 443
Reputation: 19
I have found a solution:
this is how i did:
SELECT Id,Nom,
CASE
WHEN Telephone LIKE '%$keyword%' THEN Telephone
WHEN Telephone2 LIKE '%$keyword%' THEN Telephone2
END AS MatchingColumn
FROM employes
WHERE Nom
LIKE '%$keyword%' || Prenom
LIKE '%$keyword%' || Telephone
LIKE '%$keyword%' || Telephone2
LIKE '%$keyword%'
MatchingColumn is the field name with the value where the LIKE matchs
Thanks for the help
Upvotes: 1