I wrote MySQL query, it returns results from two tables related to particular user ID (in live project each user sees own data). What I am trying to achieve is to insert search option, but can't find proper way to do it. Search should work with columns: object, name, color and age. Key which connects two tables is 'object'. Thanks if someone could point me to the right direction.
SELECT table1.user_id, table1.object, table2.name, table2.object, table2.color, table2.age
FROM table2
JOIN table1 ON table2.object = table1.object
WHERE (table1.user_id = '".$user['id']."')
ORDER BY table1.object ASC
If you want to find a string value anywhere in the field:
SELECT field1, field2 FROM table1 WHERE field1 LIKE '%VALUE%';
If you are looking for an exact match:
SELECT field1, field2 FROM table1 WHERE field1 = 'VALUE';
Bryan's answer is correct. You can either filter out in the original query or you can also filter out after the join.
SELECT table1.user_id, table1.object, table2.name, table2.object, table2.color, table2.age
FROM table2
JOIN table1 ON table2.object = table1.object
WHERE (table1.user_id = '".$user['id']."') and table2.color='red' and 'table2.age=15
ORDER BY table1.object ASC
If you want to return the joined results and then search, you can try the following:
SELECT * FROM (SELECT table1.user_id, table1.object as obj1, table2.name, table2.object, table2.color, table2.age
FROM table2
JOIN table1 ON table2.object = table1.object
WHERE (table1.user_id = '".$user['id']."'))tblA WHERE color='red' and age=15
ORDER BY obj1