So, I've got a webapp that lets users submit code. The submissions are stored in the code
table, and a couple of columns of that are Fulltext-Indexed. This is how I have been performing searches up until now.
But, users can submit their submissions with as many tags as they like - and I'd like these to be included in the search too (but, all in one query...). The tags are stored in the table tags
, and there's an intersection table called code_tags
that stores the code_id
and the tag_id
. Standard stuff.
My 'old' search query was this:
SELECT *
FROM code
WHERE MATCH (title, summary, code) AGAINST ('$searchterm')
$searchterm
was fetched via PHP $_POST.
So I tried to write a bit more of an 'advanced' query:
SELECT code.*,
code_tags.*,
tags.*,
tags.tag
FROM code, code_tags, tags
WHERE code_tags.code_id = code.id
AND tags.id = code_tags.tag_id
AND MATCH (title, summary, code) AGAINST ('$searchterm')
But all this did was return... nothing. Even when a perfectly valid search term was entered.
So I commented out the last line:
SELECT code.*, code_tags.*, tags.*, tags.tag
FROM code, code_tags, tags
WHERE code_tags.code_id = code.id
AND tags.id = code_tags.tag_id
-- AND MATCH (title, summary, code) AGAINST ('php')
This returns every submission in the database. But, the same row is repeated as many times as there are tags for it (the only difference being, the tag in each returned row).
E.G:
So, finally, I thought I'd be clever and GROUP_CONCAT the tags:
SELECT code.*, code_tags.*, tags.*, GROUP_CONCAT(tags.tag SEPARATOR ' ') AS taggroup
FROM code, code_tags, tags
WHERE code_tags.code_id = code.id
AND tags.id = code_tags.tag_id
-- AND MATCH (title, summary, code, taggroup) AGAINST ('php')`
There are two pretty big problems with this.
- With the last
AND MATCH
line commented out, only one row is returned (with all the details of the first entry in thecode
table - and taggroup lists every tag, for every submission! - With the last
AND MATCH
line included, I get the following error:Unknown column 'taggroup' in 'where clause'
- damn!
So, what am I meant to do? :S