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 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... 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 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 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. Link to comment https://forums.phpfreaks.com/topic/167969-solved-simple-question/#findComment-885975 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.