7

I am trying to make a MySQL query where I filter the records based on the search text using LIKE keyword.

For example if the user searches for Illusion Softwares where Illusion is First name and Softwares is last name so the query should search for columns FirstName, LastName and concat both and search.

I have tried this so far but it does not work for CONCAT

 Select Contacts.*, Contacts.ID as CID from Contacts left join
 website_Data  on Contacts.ID = website_Data.ContactID where
 Contacts.`FirstName` LIKE '%Illusion Softwares%' or 
 Contacts.`LastName` LIKE '%Illusion Softwares%' or
 concat(Contacts.FirstName, ' ', Contacts.LastName) 
 LIKE '%Illusion Softwares%'
 or Contacts.Email LIKE '%Illusion Softwares%' 
 order by Contacts.`Created` DESC

What am I missing? Please help me.

0

4 Answers 4

26

Your query should be like this

Select Contacts.*, Contacts.ID as CID 
from Contacts 
left join website_Data 
on Contacts.ID = website_Data.ContactID 
where CONCAT(Contacts.FirstName,' ', Contacts.LastName) LIKE '%Illusion Softwares%' 
order by Contacts.`Created` DESC 
Sign up to request clarification or add additional context in comments.

Comments

1
SELECT *,c.ID as CID 
FROM Contacts c
LEFT OUTER JOIN website_Data w ON c.ID=w.ContactID
WHERE c.Email OR CONCAT_WS(' ',c.FirstName,c.LastName) LIKE '%Illusion Softwares%'
ORDER BY c.Created DESC;

Comments

0

you can try this also, this worked for me,

$this->db->or_like('CONCAT(Contacts.FirstName,' ', Contacts.LastName)',$search);

1 Comment

Use CONCAT_WS() instead CONCAT()
0

Use CONCAT_WS in your query:

$this->db->or_like('CONCAT_WS(Contacts.FirstName,' ', Contacts.LastName)',$search);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.