2011/03/03

sql get parent cat by child id

原來呢個網有教



我再改一下


#get the path by child id, only one row,
SELECT t1.name, t2.name, t3.name
FROM category AS t1
LEFT JOIN category AS t2 ON t2.parent = t1.category_id
LEFT JOIN category AS t3 ON t3.parent = t2.category_id
WHERE (t3.category_id = [child_id])


#get the path by child id, row number = member of family
SELECT t1.* FROM category AS t1
LEFT JOIN category AS t2 ON t2.parent = t1.category_id
LEFT JOIN category AS t3 ON t3.parent = t2.category_id
WHERE 
(t1.category_id = [child_id] OR t2.category_id = [child_id] OR t3.category_id = [child_id])
 ORDER BY t1.LEVEL

沒有留言:

發佈留言