Вопрос по LEFT JOIN...

Amiddio

Новичок
Вопрос по LEFT JOIN...

Есть простая таблица:

CREATE TABLE `tree` (
`id` int(11) NOT NULL auto_increment,
`parent` int(11) NOT NULL default '0',
`text` text NOT NULL,
PRIMARY KEY (`id`)
)

В parent храниться id родителя. У корневой записи parent = 0 и т.д.
Формирую запрос в цикле и вывожу потомков какой-нибуть записи.
Запрос получается такой (я его формирую в цикле в зависимости от уровней вложености):

SELECT
t0000.id as `0.id`, t0000.parent as `0.parent`, t0000.text as `0.text`,
t0001.id as `1.id`, t0001.parent as `1.parent`, t0001.text as `1.text`,
...
FROM
tree t0000
LEFT JOIN tree t0001 ON (t0000.id=t0001.parent)
LEFT JOIN tree t0002 ON (t0001.id=t0002.parent)
...
WHERE t0000.id=1

Вопрос: как сформировать (переделать) запрос, который будет выводить не потомков а предков?
 

matross

Новичок
Re: Вопрос по LEFT JOIN...

Делаешь все тоже только бежишь не вниз по дереву, а вверх!!!
 

Amiddio

Новичок
Пробовал... пишет ошибку: Cross dependency found in OUTER JOIN; examine your ON conditions
 

Amiddio

Новичок
SELECT
t0000.id as `0.id`, t0000.parent as `0.parent`, t0000.text as `0.text`,
t0001.id as `1.id`, t0001.parent as `1.parent`, t0001.text as `1.text`,
t0002.id as `2.id`, t0002.parent as `2.parent`, t0002.text as `2.text`,
t0003.id as `3.id`, t0003.parent as `3.parent`, t0003.text as `3.text`,
t0004.id as `4.id`, t0004.parent as `4.parent`, t0004.text as `4.text`
FROM
tree t0000
LEFT JOIN tree t0004 ON (t0003.id=t0004.parent)
LEFT JOIN tree t0003 ON (t0002.id=t0003.parent)
LEFT JOIN tree t0002 ON (t0001.id=t0002.parent)
LEFT JOIN tree t0001 ON (t0000.id=t0001.parent)
WHERE t0000.id=1

-~{}~ 27.05.05 17:01:

... я не очень силен в запросах :)

-~{}~ 27.05.05 18:01:

Вроде сделал:

SELECT
t0000.id as `0.id`, t0000.parent as `0.parent`, t0000.text as `0.text`,
t0001.id as `1.id`, t0001.parent as `1.parent`, t0001.text as `1.text`,
t0002.id as `2.id`, t0002.parent as `2.parent`, t0002.text as `2.text`,
t0003.id as `3.id`, t0003.parent as `3.parent`, t0003.text as `3.text`,
t0004.id as `4.id`, t0004.parent as `4.parent`, t0004.text as `4.text`
FROM
tree t0004
LEFT JOIN tree t0003 ON (t0004.parent=t0003.id)
LEFT JOIN tree t0002 ON (t0003.parent=t0002.id)
LEFT JOIN tree t0001 ON (t0002.parent=t0001.id)
LEFT JOIN tree t0000 ON (t0001.parent=t0000.id)
WHERE t0004.id=21
 
Сверху