schilly Posted May 12, 2010 Share Posted May 12, 2010 If I have a table of items with two columns set for categories, say cat1 and cat2 so I can list an item in multiple categories. What's the best way to get a list of all the unique categories? Some categories may only be listed in cat1 and some only in cat2 and some in both. ex. cat1 | cat2 cat | dog house | apartment dog | cat What query would I use to return: cat house apartment dog (no particular order) I'm trying to use combinations of DISTINCT but it doesn't seem to work. Quote Link to comment https://forums.phpfreaks.com/topic/201563-distinct-values-from-multiple-columns/ Share on other sites More sharing options...
otuatail Posted May 12, 2010 Share Posted May 12, 2010 You need to reserch your table layout. Why can cat be in category 1 and somwhere else be in category 2. Question! how is this table being populated? by user input? Desmond. Quote Link to comment https://forums.phpfreaks.com/topic/201563-distinct-values-from-multiple-columns/#findComment-1057452 Share on other sites More sharing options...
schilly Posted May 12, 2010 Author Share Posted May 12, 2010 Thanks for the reply. It's not user inputted. well say you have a primary and secondary categories. one product will want to be listed in cat A as their primary but another product will want to be listed in cat A as their secondary giving you the same category value in both columns. Quote Link to comment https://forums.phpfreaks.com/topic/201563-distinct-values-from-multiple-columns/#findComment-1057462 Share on other sites More sharing options...
DavidAM Posted May 13, 2010 Share Posted May 13, 2010 SELECT DISTINCT cat1 FROM table UNION DISTINCT SELECT DISTINCT cat2 FROM table something like that, I'm not sure if all of the DISTINCTs are needed Quote Link to comment https://forums.phpfreaks.com/topic/201563-distinct-values-from-multiple-columns/#findComment-1057496 Share on other sites More sharing options...
schilly Posted May 13, 2010 Author Share Posted May 13, 2010 SELECT DISTINCT cat1 FROM table UNION DISTINCT SELECT DISTINCT cat2 FROM table something like that, I'm not sure if all of the DISTINCTs are needed awesome. just what i needed. thanks so much. Quote Link to comment https://forums.phpfreaks.com/topic/201563-distinct-values-from-multiple-columns/#findComment-1057779 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.