waynew Posted July 29, 2009 Share Posted July 29, 2009 I know that this is simple, but I've never had the need to know how to do this until now. I need to print out a list of location names from a database. I have an advert table, and each advert is linked to one location. So, how do I print out a list of location names while counting how many adverts are in that location category so that I can have something like: Utah (3223) Washington (8782) Florida (6799) etc Quote Link to comment https://forums.phpfreaks.com/topic/167969-solved-simple-question/ Share on other sites More sharing options...
fooDigi Posted July 29, 2009 Share Posted July 29, 2009 you'll have to join the two tables group by the location, either by name or it's id, and then count() the advert unique id field... i can give more precise code if you provide the table definitions... Quote Link to comment https://forums.phpfreaks.com/topic/167969-solved-simple-question/#findComment-885959 Share on other sites More sharing options...
waynew Posted July 29, 2009 Author Share Posted July 29, 2009 Basic overview of my two tables: Location: id Advert: id location_id Quote Link to comment https://forums.phpfreaks.com/topic/167969-solved-simple-question/#findComment-885962 Share on other sites More sharing options...
fooDigi Posted July 29, 2009 Share Posted July 29, 2009 this code *should* work, i didn't test it... select Location.id, count(Advert.id) from Location inner join Advert on Location.id = Advert.location_id group by Location.id Quote Link to comment https://forums.phpfreaks.com/topic/167969-solved-simple-question/#findComment-885969 Share on other sites More sharing options...
waynew Posted July 29, 2009 Author Share Posted July 29, 2009 Worked perfectly, thanks a million. Quote Link to comment https://forums.phpfreaks.com/topic/167969-solved-simple-question/#findComment-885975 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.