从mysql中的同一个表中获取id及其相关的所有子id

时间:2015-11-25 10:35:59

标签: mysql

我想得到与menu_id相关的menu_id,即menu_id 3应返回

3,4,6,11

以下是我的表:

Table

1 个答案:

答案 0 :(得分:1)

我认为此查询适合您:

SELECT @id := (SELECT ms_menu_id FROM ms_menu WHERE ms_menu_parent = @id) 
   AS menu_id  FROM (SELECT @id := 3) vars STRAIGHT_JOIN ms_menu WHERE 
@id !=0  union all (select ms_menu_id from ms_menu where ms_menu_id=3) 
order by menu_id ASC limit 1, 18446744073709551615

结果应为:3,4,6,11