I am fairly new with advanced MySQL commands, I have database tables with multiple relationships. I have an advanced search feature that must match Keywords to a few fields (most being in the Assets table but a couple in the User table). When I execute the following Query for desk it returns the same row multiple times, while it should do it once.
SELECT
a.id, a.asset_id, a.name, a.serial_number, a.category_id, a.status_id, a.user_id, a.location_id
FROM
assets a, users u
WHERE
(a.asset_ID LIKE '%desk%' OR a.name LIKE '%desk%' OR (u.first_name LIKE '%desk%' OR u.last_name LIKE '%desk%')) AND
a.serial_number LIKE '%%' AND
a.category_id='2'
LIMIT 25 OFFSET 5450
You have a cartesian product in your query. You should JOIN assets and users, e.g. with
or like this
UPDATE: Your comment shows that you actually want to “left outer join” the users table. This means, that all assets are in the result set regardless if there exists a matching user:
Read more about joining tables here: http://dev.mysql.com/doc/refman/5.5/en/join.html