playaz Posted April 20, 2006 Share Posted April 20, 2006 Hi guys,I am having problems with self joins - I've used inner joins before on seperate tables but never when only one is used. I have a category table which looks as follows:Below is an example of the database[a href=\"http://imageshack.us\" target=\"_blank\"][img src=\"http://img208.imageshack.us/img208/1307/category2jd.jpg\" border=\"0\" alt=\"IPB Image\" /][/a]I want to use a join and CONCAT to make it simplier to understand which category a sub-category belongs to..egCar & Motorbike are BOTH level 1 (main category)While 4x4, Rally and Premium are all level 2 (sub-categories) - these are sub categories of the Car category as defined by the parent_id field so for instance using the CONCAT i want to somehow get it looking like this..Car -> 4x4Car -> RallyCar -> PremiumCan anyone help me out im pulling my hair out :)Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/7936-help-on-joins-within-the-same-table/ Share on other sites More sharing options...
fenway Posted April 21, 2006 Share Posted April 21, 2006 No need to use concat...[code]SELECT parent.category AS category, sub.category AS subcategoryFROM categories AS parentJOIN categories AS sub ON ( parent.category_id = sub.parent_id )WHERE parent.category_id <> sub.category_idORDER BY parent.category_id, sub.category_id[/code]The WHERE clause simply prevents the parent categories from showing up in the result set, and you can ORDER BY whatever you want (i.e. the name itself).Hope that helps. Quote Link to comment https://forums.phpfreaks.com/topic/7936-help-on-joins-within-the-same-table/#findComment-29373 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.