MySQL:仅在找到子项时才添加父行(使用LIKE,相同的表)

时间:2015-02-10 18:22:53

标签: mysql parent children relation

我有这样的表:



id   |    type      |    parent   |   alias
----------------------------------------------
1    |    Type1     |    0        |   animals,fauna
2    |    Subtype11 |    1        |   cat
3    |    Subtype12 |    1        |   goat
4    |    Subtype13 |    1        |   bird,owl
5    |    Type2     |    0        |
6    |    Type3	    |    0        |
7    |    Subtype31 |    6        |   type2 for example,other aliasname
8    |    Subtype32 |    6        |



 此表用于实时搜索提示系统及其简单的父子关系 - 不再是级别。

我使用简单查询:

SELECT id,type FROM table_name WHERE type LIKE '%something%' OR alias LIKE '%something%'

它按照我的预期工作,但我需要扩展它。我不仅要接受单个孩子的行,还要接受他们的父母。

EG。 如果我正在寻找:

(...) WHERE type LIKE '%cat%' OR alias LIKE '%cat%' OR type LIKE '%goat%' OR alias LIKE '%goat%'

我需要收到的不是单身:

id   |    type      |    parent   |   alias
----------------------------------------------
2    |    Subtype11 |    1        |   cat
3    |    Subtype12 |    1        |   goat

还有他们的父母(如果有的话):

id   |    type      |    parent   |   alias
----------------------------------------------
1    |    Type1     |    0        |   animals,fauna
2    |    Subtype11 |    1        |   cat
3    |    Subtype12 |    1        |   goat

正如上面所述,因为如果父= 0,我会稍后检查该行将该行标记为子组。

还有一个技巧 - 我也可以通过父母进行搜索,例如:。

(...) WHERE type LIKE '%Type2%' OR alias LIKE '%Type2%'

应该返回:

id   |    type      |    parent   |   alias
----------------------------------------------
5    |    Type2     |    0        |
6    |    Type3     |    0        |
7    |    Subtype31 |    6        |   type2 for example,other aliasname

当然会有大约500行具有不同类型/别名 - 我需要始终与父母一起返回它们。

我虽然很容易做到,但它不是(或者只是我有一些"脑损伤":D)

3 个答案:

答案 0 :(得分:1)

您可以执行此操作,在子记录到父记录的表上执行自联接。添加两个记录(父/子)的条件。然后根据需要为要显示的字段添加别名。

SELECT ch.id, ch.type, prnt.id AS parent_id, prnt.type AS parent_type
FROM   table_name AS ch
       JOIN table_name as prnt
       ON ch.parent = prnt.id
WHERE  (ch.type LIKE '%something%' OR ch.alias LIKE '%something%')
       AND  -- Or "OR" depending on what you are trying to do
       (prnt.type LIKE '%Type2%' OR prnt.alias LIKE '%Type2%');

答案 1 :(得分:0)

您可以使用别名连接同一个表,并根据父过滤器或子过滤器传递您的where子句。

请参阅以下查询而不使用where子句:

select c.id, c.type,c.alias,p.id,p.type,p.alias from stack c left join stack p on(c.parent=p.id)

+------+-----------+-----------------+------+--------+---------+
| id   | type      | alias           | id   | type   | alias   |
+------+-----------+-----------------+------+--------+---------+
|    2 | Subtype11 | cat             |    1 | Type1  | animals |
|    3 | Subtype12 | goat            |    1 | Type1  | animals |
|    4 | Subtype13 | bird            |    1 | Type1  | animals |
|    5 | Type2     |                 | NULL | NULL   | NULL    |
|    6 | Type3     |                 | NULL | NULL   | NULL    |
|    7 | Subtype31 | type2forexample |    6 | Type3  |         |
|    8 | Subtype32 | Type 32 example |    6 | Type3  |         |
|    1 | Type1     | animals         | NULL | NULL   | NULL    |
+------+-----------+-----------------+------+--------+---------+

请参阅以下有关子过滤器的where子句的查询:

select c.id, c.type,c.alias,p.id,p.type,p.alias from stack c left join stack p on(c.parent=p.id) where c.alias like '%cat%'

+------+-----------+-------+------+-------+---------+
| id   | type      | alias | id   | type  | alias   |
+------+-----------+-------+------+-------+---------+
|    2 | Subtype11 | cat   |    1 | Type1 | animals |
+------+-----------+-------+------+-------+---------+

如果您在下面查询没有输出。儿童元素缺失的地方。所以,不会提取父母。

select c.id, c.type,c.alias,p.id,p.type,p.alias from stack c left join stack p on(c.parent=p.id) where c.alias like '%cow%';

答案 2 :(得分:0)

@gubs的答案并不完全符合我的假设,但给了我另一个解决这个问题的机会。我可以按照建议在每行设置父ID或NULL(如果IT是父级),而不是使用父级获取一行。

现在我的查询

SELECT p.id AS parent_id,p.type AS parent_type,c.id, c.type 
FROM table_name c 
  LEFT JOIN table_name p 
  ON (c.parent=p.id) 
WHERE 
  c.type LIKE '%cat%' 
OR 
  c.alias LIKE '%cat%'

返回列

   parent_id   |    parent_type      |    id   |   type
----------------------------------------------------------

所以我可以检查parent_id是否为NULL并从'type'列而不是'parent_type'列获取组的名称。

这个答案让我满意(它与我的真实数据库完美配合),所以我会接受它:)

感谢您的帮助。