You will have to use as many selects as necessary, i.e., until you come to a category which has no parent category.
If you KNOW the number of subcategories -- like it's always
category
subcat
subsubcat
product
then you can get the hierarchy by aliasing category
select a.categoryname, b.categoryname, c.categoryname, from category a, category b, category c
etc.
for as many sub categories as you have
If you don't know the number for subcategories, you 'll have to use a while loop that loops through selecting parent categories until it finds the top-level category. This should not take long if you've indexed the ids, which is likely you've done.