I am using this hierarchy format to build a hierarchy of categories. I am trying to build a search to search the tips table using a full text index. Doing a search works fine, but I would like to get a hierarchy column as from the categories table, where each returned row is separated by a /
.
Example:
Lets say the return looks like this:
+---------------+-------------+
| category_name | title |
+---------------+-------------+
| Computers | How to jump |
| Video Games | How to jump |
| Super Mario | How to jump |
+---------------+-------------+
Instead, how can I get the return to look like this:
+-----------------------------------+-------------+
| category_path | title |
+-----------------------------------+-------------+
| Computers/Video Games/Super Mario | How to jump |
+-----------------------------------+-------------+
Categories Table
mysql> describe categories;
+---------------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+----------+------+-----+---------+----------------+
| category_id | int(11) | NO | PRI | NULL | auto_increment |
| category_name | char(60) | NO | | NULL | |
| lft | int(11) | NO | | NULL | |
| rgt | int(11) | NO | | NULL | |
+---------------+----------+------+-----+---------+----------------+
4 rows in set (0.07 sec)
Tips Table
mysql> describe tips;
+-------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+----------------+
| tip_id | int(11) | NO | PRI | NULL | auto_increment |
| category_id | int(11) | NO | MUL | NULL | |
| title | varchar(100) | NO | MUL | NULL | |
| tip | text | NO | | NULL | |
+-------------+--------------+------+-----+---------+----------------+
4 rows in set (0.07 sec)
Here is what I have for a query so far
select * from tips,
categories AS node,
categories AS parent
where match (tips.title, tips.tip) against (? in boolean mode)
AND node.lft BETWEEN parent.lft AND parent.rgt
AND node.category_name = ?
AND parent.lft != 1
ORDER BY node.lft
Here is my final result:
select title, group_concat(parent.category_name order by parent.lft separator '/') as category_path
from tips, categories as node, categories as parent
where match (tips.title, tips.tip) against ('button' in boolean mode)
and tips.category_id = node.category_id
and node.lft between parent.lft and parent.rgt
and parent.lft != 1
group by title;