分类数据库
id name parent_id slug
----------------------------------
1 Men 0 men
2 Shoes 1 shoes
3 Sports 2 sports
4 Women 0 women
5 Shoes 4 shoes
6 Sports 5 sports
假设我在domain/category/men/shoes/sports
$last_slug = end((explode('/', Request::url())));
在这里我可以得到sports
最后一个查询
问题 :
如何动态检索所有父母蛞蝓,所以如果蛞蝓我可以选择这样的东西depth = 3
?
SELECT t1.name AS level_one,
t2.name AS level_two,
t3.name AS level_three,
t3.id AS requested_id
FROM categories AS t1
LEFT JOIN categories AS t2 ON t2.parent_id = t1.id
LEFT JOIN categories AS t3 ON t3.parent_id = t2.id
WHERE t1.slug = 'men'
AND t2.slug = 'shoes'
AND t3.slug = 'sports'
LIMIT 1