conmen
conmen

Reputation: 2417

PHP search keywords in multiple table column

I have below search script to perform multiple column search in table, but the results is not as expected, few of data rows are show with status = new and company name = demo, can someone please point out what is wrong for below query? what is more accurate query to perform a search with these conditions?

SELECT * FROM messageboard AS m LEFT JOIN users AS u ON m.author_id=u.user_id 
WHERE m.status='approved' 
AND u.user_email LIKE '%demo%' 
OR u.company_name LIKE '%demo%' 
OR m.subject LIKE '%demo%' 
ORDER BY m.posted_time DESC

Thanks a lot.

Upvotes: 0

Views: 848

Answers (2)

Adam Szabo
Adam Szabo

Reputation: 11412

I suggest organize your AND and OR conditions with brackets.

Upvotes: 0

open source guy
open source guy

Reputation: 2787

SELECT * FROM messageboard AS m LEFT JOIN users AS u ON m.author_id=u.user_id 
WHERE m.status='approved' 
AND (u.user_email LIKE '%demo%' 
OR u.company_name LIKE '%demo%' 
OR m.subject LIKE '%demo%' )
ORDER BY m.posted_time DESC

Upvotes: 1

Related Questions