Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. SELECT wc1.id AS category_id, wc1.name AS category_name,
  2. if(wc3.parent_id is not null, wc4.name,
  3. if(wc2.parent_id is not null, wc3.name,
  4. if(wc1.parent_id is not null, wc2.name, wc1.name))) as ultimate_parent
  5. FROM CATEGORIES wc1
  6. LEFT JOIN categories wc2 ON wc1.parent_id = wc2.id
  7. LEFT JOIN categories wc3 ON wc2.parent_id = wc3.id
  8. LEFT JOIN categories wc4 ON wc3.parent_id = wc4.id;
  9.  
  10. SELECT wc1.id AS category_id, wc1.name AS category_name,
  11. if wc3.parent_id is not null then wc4.name,
  12. if wc2.parent_id is not null then wc3.name,
  13. else wc1.parent_id is not null Then wc2.name, wc1.name as ultimate_parent -- I am having issues with splitting the last if clause
  14. FROM CATEGORIES wc1
  15. LEFT JOIN categories wc2 ON wc1.parent_id = wc2.id
  16. LEFT JOIN categories wc3 ON wc2.parent_id = wc3.id
  17. LEFT JOIN categories wc4 ON wc3.parent_id = wc4.id;
  18.  
  19. SELECT wc1.id AS category_id, wc1.name AS category_name,
  20. (case when wc3.parent_id is not null then wc4.name
  21. when wc2.parent_id is not null then wc3.name
  22. when wc1.parent_id is not null then wc2.name
  23. else wc1.name
  24. end) as ultimate_parent
  25. FROM CATEGORIES wc1 LEFT JOIN
  26. categories wc2 ON wc1.parent_id = wc2.id LEFT JOIN
  27. categories wc3 ON wc2.parent_id = wc3.id LEFT JOIN
  28. categories wc4 ON wc3.parent_id = wc4.id;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement