Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Combining queries to increase efficiency

Status
Not open for further replies.

emperorevil

Programmer
Apr 28, 2002
23
0
0
US
Here's the situation: It's a simple news script, which is based off of a forum, all in one table. The first column is forumid, which is always 1. The next is topicid, which is different for each news post. The postid is 1 for each topicid for the main message, and continues on for each reply to that message. The rest is standard information:

So I would get the sql table for that starting off by doing this:
--------
$query="SELECT topicid, username, subject, timestamp, message, FROM posts WHERE forumid=1 AND postid=1 ORDER BY topicid DESC LIMIT 12";
$result=mysql_query($query);
while($row=mysql_fetch_array($result)){
// output the information //
}
-------

Then I want to tell the viewer how many replies to each news post there are. So I would count the number of posts in the news forum for that topic by placing this inside the while() loop:
-------
$query="select count(*) from posts where forumid=1 and topicid='$row[1]'";
$result=mysql_query($query);
$numofreplies=mysql_fetch_array($result);
$numofreplies[0]--;
-------

Now here's the tricky part. How can I incorporate this extra query I have to run into the original query? I want the number of replies per topic to be another column. That way this page only has 1 query it has to execute instead of a bunch.

I hope I explained it well..
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top