Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- SELECT wc1.id AS category_id, wc1.name AS category_name,
- if(wc3.parent_id is not null, wc4.name,
- if(wc2.parent_id is not null, wc3.name,
- if(wc1.parent_id is not null, wc2.name, wc1.name))) as ultimate_parent
- FROM CATEGORIES wc1
- LEFT JOIN categories wc2 ON wc1.parent_id = wc2.id
- LEFT JOIN categories wc3 ON wc2.parent_id = wc3.id
- LEFT JOIN categories wc4 ON wc3.parent_id = wc4.id;
- SELECT wc1.id AS category_id, wc1.name AS category_name,
- if wc3.parent_id is not null then wc4.name,
- if wc2.parent_id is not null then wc3.name,
- 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
- FROM CATEGORIES wc1
- LEFT JOIN categories wc2 ON wc1.parent_id = wc2.id
- LEFT JOIN categories wc3 ON wc2.parent_id = wc3.id
- LEFT JOIN categories wc4 ON wc3.parent_id = wc4.id;
- SELECT wc1.id AS category_id, wc1.name AS category_name,
- (case when wc3.parent_id is not null then wc4.name
- when wc2.parent_id is not null then wc3.name
- when wc1.parent_id is not null then wc2.name
- else wc1.name
- end) as ultimate_parent
- FROM CATEGORIES wc1 LEFT JOIN
- categories wc2 ON wc1.parent_id = wc2.id LEFT JOIN
- categories wc3 ON wc2.parent_id = wc3.id LEFT JOIN
- categories wc4 ON wc3.parent_id = wc4.id;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement