0

Its my table:

CREATE TABLE IF NOT EXISTS `category` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(100) CHARACTER SET utf8 COLLATE utf8_persian_ci NOT NULL,
`parentid` int(11) NOT NULL DEFAULT '0',
 PRIMARY KEY (`id`))

Where parentid Refrenced to this table. I have this data:

Id    |    Name     |      parentid
1          Cumputer        0
2          MotherBoard     1
3          Cpu             2
4          Cpu cooling Fan 3
5          Monitor         1

Now, I want to select Cpu cooling fan's Id and it's all parents:

1
2
3

Something like this:

SELECT a.id, b.id as list FROM `category` a INNER JOIN category b ON a.id=b.parentid

But it doesn't work.

halfer
  • 19,824
  • 17
  • 99
  • 186
user2169502
  • 23
  • 1
  • 4
  • "all parents"? That requires recursion, especially if you don't have a limit to how "deep" the tree can go. mysql doesn't support recursive queries. you can fake it, as you have, with any number of self-joins, but that's just a hack, not a "solution". – Marc B Jan 14 '16 at 21:23
  • @Marc Ok thanks. i'll try to solve it in Php by multiple selects. Thank you – user2169502 Jan 14 '16 at 21:32
  • 1
    Possible duplicate of [Mysql select recursive get all child with multiple level](http://stackoverflow.com/questions/28363893/mysql-select-recursive-get-all-child-with-multiple-level) – PM 77-1 Jan 14 '16 at 22:20

0 Answers0