I have an SQL query:
SELECT *
FROM user, user_company, company
WHERE user.user_id = user_company.user_id AND
user_company.company_id = '$company_id' AND
user_company.company_id = company.company_id AND
CONCAT(user.user_first_name, ' ', user.user_last_name) LIKE '%$l%' OR user.user_email LIKE '%$l%'
LIMIT 5
This seems to work on returning the values whether the user inputs something that's similar to the name or the email, however a flaw in this is if the user inputs the email, the record will be duplicated.
user_company doesn't contain any email data; it's only contained in the user table.
How do I stop that from happening?
Never use commas in the FROM
clause. Always use proper, explicit, standard JOIN
syntax.
Your problem is lack of parentheses. Try:
SELECT *
FROM user u JOIN
user_company uc
ON (user_id) JOIN
company c
ON (company_id)
WHERE uc.company_id = '$company_id' AND
(CONCAT(u.user_first_name, ' ', u.user_last_name) LIKE '%$l%' OR
u.user_email LIKE '%$l%'
)
LIMIT 5