【发布时间】:2013-05-26 20:21:29
【问题描述】:
我正在使用this hierarchy 格式来构建类别层次结构。我正在尝试构建一个搜索来使用全文索引搜索提示表。进行搜索工作正常,但我想从类别表中获取层次结构列,其中每个返回的行由 / 分隔。
示例:
假设返回如下所示:
+---------------+-------------+
| 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 |
+-----------------------------------+-------------+
类别表
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)
提示表
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)
这是我目前的查询
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
这是我的最终结果:
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;
【问题讨论】:
-
一个 SQLfiddle 会有所帮助... :)