【问题标题】:Mysql Hierarchy PathMysql层次结构路径
【发布时间】: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;

【问题讨论】:

标签: mysql hierarchy


【解决方案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;

然后你交叉手指。此查询未指定排序。如果我假设原始结果具有指定排序的 idcreationdatedepthsomething,那么我可以这样做:

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

【讨论】:

  • 那行得通!这是我的小提琴:sqlfiddle.com/#!2/cffda/6 有什么理由让我获得额外的类别?我得到了这个:Cooking/Video Games/Computers/Video Games,我希望得到这个:Computers/Video Games
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-17
  • 2013-10-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多