Jump to content


SQL query help for PHP message board

  • Please log in to reply
2 replies to this topic

#1 Forge102

  • Members
  • Pip
  • Newbie
  • 9 posts

Posted 20 December 2005 - 04:37 PM

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]



#2 ryanlwh

  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 21 December 2005 - 05:56 AM

[!--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--]
Please use EDIT * 100...
Please use
or [php] * 1000...


#3 fenway

  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 23 December 2005 - 10:07 PM

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

Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users