MySQL "table_a":
+----+-------+
| id | title |
+----+-------+
Now I can do a search like so:
$term = mysql_real_escape_string($_GET['term']);
mysql_query("SELECT * FROM `table_a`
WHERE MATCH(`title`) AGAINST('$term' IN BOOLEAN MODE) LIMIT 0,5");
However I want to add another compontent.
MySQL "table_b":
+----+----------+
| id | category |
+----+----------+
MySQL "table_c":
+------------+------------+
| table_a_id | table_b_id |
+------------+------------+
So, I want to look for a specific category
in table_b that is linked to table_a according to table_c.
BUT, it isn't always the case that a category
is linked to table_a (so it could happen that there just aren't any entries in table_c that are connected to table_a).
AND, if there is a category
linked to table_a I want to be able to either search for the title
in table_a or the category
in table_b (so both should be possible, so category
shouldn't overrule title
, or the other way around). But if that's not possible, then title
should overrule category
.
Here's what I came up with so far, but the problem is
a) it doesn't work
b) it doesn't include the title OR category
as I had just explained
$term = mysql_real_escape_string($_GET['term']);
mysql_query("SELECT a.*, LEFT JOIN (SELECT c.table_a_id FROM table_b AS b
, table_c AS c WHERE b.category = '$term' AND b.id = c.table_b_id)
AS d ON d.table_a_id = a.id FROM table_a AS a
WHERE MATCH(a.title) AGAINST('$term' IN BOOLEAN MODE) LIMIT 0,5");
Any help would be greatly appreciated.