Forge102 Posted December 20, 2005 Share Posted December 20, 2005 Hi all, I've created a message board for my website. I am trying to display next to the list of post titles the number of reviews that have been posted for that particular post title. The query below selects all the post titles for a user selected category: SELECT DISTINCT post_title FROM tablename WHERE post_category = $user_selected_post_category What I want to do next is within a for loop display the post_titles from the above query and next to the post_titles put the number of replies. In my database, each reply as a unique id called post_id. What I think is to COUNT all the post_ids for that post_title. The below query shows this: SELECT COUNT(post_ids) AS no_of_posts FROM tablename WHERE post_title = $post_title_from_above query. How can I combine these 2 queries on one page to display '$post_title' and '$no_of_posts' within one for loop also on the same page. The message board can be found at [a href=\"http://www.mediacritic.co.uk/messageboard2.php?post_category=Films\" target=\"_blank\"]http://www.mediacritic.co.uk/messageboard2..._category=Films[/a] Thanks Forge Quote Link to comment https://forums.phpfreaks.com/topic/3078-sql-query-help-for-php-message-board/ Share on other sites More sharing options...
ryanlwh Posted December 21, 2005 Share Posted December 21, 2005 [!--sql--][div class=\'sqltop\']SQL[/div][div class=\'sqlmain\'][!--sql1--][span style=\'color:blue;font-weight:bold\']SELECT[/span] DISTINCT post_title, COUNT(post_ids) AS no_of_posts FROM tablename WHERE post_category = '$user_[span style=\'color:blue;font-weight:bold\']selected_post_category'[/span] GROUP BY post_title [!--sql2--][/div][!--sql3--] Quote Link to comment https://forums.phpfreaks.com/topic/3078-sql-query-help-for-php-message-board/#findComment-10293 Share on other sites More sharing options...
fenway Posted December 23, 2005 Share Posted December 23, 2005 Why not try the following (UNTESTED): SELECT post_title, ( SELECT COUNT(post_ids) FROM tablename WHERE post_title = t1.post_title ) AS no_of_posts FROM tablename WHERE post_category = $user_selected_post_category GROUP BY post_title Quote Link to comment https://forums.phpfreaks.com/topic/3078-sql-query-help-for-php-message-board/#findComment-10340 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.