86Stang Posted November 25, 2009 Share Posted November 25, 2009 I've got the following table structure: ID int(10) auto_increment Phrase varchar(10) What I need to do is output a list of all distinct phrases but when there is more than one of the same phrase put a (x) next to it where x is the count of that phrase. In other words, it should look something like: Cow Dog (x3) Chicken Zebra (x2) As always, any help is much appreciated. Loving the new look of the forums too! SMF for the win!! Quote Link to comment https://forums.phpfreaks.com/topic/182936-query-that-shows-count-of-matching-columns/ Share on other sites More sharing options...
rajivgonsalves Posted November 25, 2009 Share Posted November 25, 2009 you can use count and group by with concat Quote Link to comment https://forums.phpfreaks.com/topic/182936-query-that-shows-count-of-matching-columns/#findComment-965588 Share on other sites More sharing options...
Maq Posted November 25, 2009 Share Posted November 25, 2009 Try something along of the lines of: SELECT CONCAT(Phase, IF(count(*)>1,CONCAT(' x',count(*)),'')) AS new_name FROM [table_name] GROUP BY Phase; Quote Link to comment https://forums.phpfreaks.com/topic/182936-query-that-shows-count-of-matching-columns/#findComment-965637 Share on other sites More sharing options...
fenway Posted November 25, 2009 Share Posted November 25, 2009 But this kind of thing should never be done in the query itself. Get the value, get the count, and have the script output them as you please. Quote Link to comment https://forums.phpfreaks.com/topic/182936-query-that-shows-count-of-matching-columns/#findComment-965680 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.