Jump to content

Archived

This topic is now archived and is closed to further replies.

Forge102

SQL query help for PHP message board

Recommended Posts

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

Share this post


Link to post
Share on other sites

[!--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--]

Share this post


Link to post
Share on other sites

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

 

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.