neo926 Posted March 14, 2006 Share Posted March 14, 2006 What a fun topic name that is. Okay, this is the last big challenge for me and my site. As I've said in my earlier threads, I have a db that contains columns that are posted for a sports site. One of the fields in my table that contains the column information is the specific sport being written about. Another field contains the name of the author of the column. So, what I would like to do is query the db to achieve the following display result:Table contains:Sport AuthorNFL MarkMLB JoeNFL MattNBA JohnMLB SteveQuery displays:NFLMarkMattMLBJoeSteveNBAJohnNow I know I can use the SELECT DISTINCT(sport) to display each sport individually. But can this be used in conjunction somehow with another function to get the names of the authors of said sport to display underneath? I would appreciate any help on this matter. Quote Link to comment https://forums.phpfreaks.com/topic/4944-displaying-multiple-instances-of-a-field-under-separate-distinct-field/ Share on other sites More sharing options...
wickning1 Posted March 14, 2006 Share Posted March 14, 2006 Best you can do is use ORDER BY sport, authorThat way you get:MLB JoeMLB SteveNBA JohnNFL MarkNFL MattYou'll have to do some post processing in PHP to make sure you print, for instance, MLB only once. Something like:[code]while ($row = mysql_fetch_assoc()) { if ($save_sport != $row['sport']) { echo $row['sport'] . "<br>\n"; $save_sport = $row['sport']; } echo $row['author'] . "<br>\n";}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/4944-displaying-multiple-instances-of-a-field-under-separate-distinct-field/#findComment-17454 Share on other sites More sharing options...
neo926 Posted March 14, 2006 Author Share Posted March 14, 2006 You have totally just jumped about 1,000 spots up on my 'Cool' list. Outstanding! Thank you so much! Quote Link to comment https://forums.phpfreaks.com/topic/4944-displaying-multiple-instances-of-a-field-under-separate-distinct-field/#findComment-17468 Share on other sites More sharing options...
fenway Posted March 15, 2006 Share Posted March 15, 2006 Actually, the "best" you can do is to use the GROUP_CONCAT() function (4.1+):[code]SELECT sport, GROUP_CONCAT( DISTINCT author) AS authors GROUP BY sport[/code]Hope that helps. Quote Link to comment https://forums.phpfreaks.com/topic/4944-displaying-multiple-instances-of-a-field-under-separate-distinct-field/#findComment-17632 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.