1

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

hierarchy

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;
4

1 回答 1

1

让我回答你的问题。如果你有这样的数据:

+---------------+-------------+
| category_name | title       |
+---------------+-------------+
| Computers     | How to jump |
| Video Games   | How to jump |
| Super Mario   | How to jump |
+---------------+-------------+

像这样得到它:

+-----------------------------------+-------------+
| category_path                     | title       |
+-----------------------------------+-------------+
| Computers/Video Games/Super Mario | How to jump |
+-----------------------------------+-------------+

你会这样做:

select group_concat(category_name separator '/'), title
from t
group by title;

然后你交叉手指。此查询未指定排序。如果我假设原始结果有一个id或一个creationdate或一个指定排序depth东西,那么我可以这样做:

select group_concat(category_name separator '/' order by id), title
from t
group by title;
于 2013-05-26T20:45:15.767 回答