Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8829793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:54:58+00:00 2026-06-14T07:54:58+00:00

I am improving my php script which has two simple sql queries. What Im

  • 0

I am improving my php script which has two simple sql queries. What Im trying to do is to combine the two queries into one.

1st query:

SELECT categories.*, entries.* 
FROM categories, entries 
WHERE entries.cat_id = categories.cat_id 
ORDER BY dateposted ASC 
LIMIT 5;

2nd query:

SELECT comments.* 
FROM comments 
WHERE comments.entry_id = '. $row['id'].';

These two work great when separate. I just need to combine them into one(still simple, no UNION or INNER JOINs please) and possibly count a number of comments for a particular entry right in the query.
Also, my “comments” table has five columns (comment_id, post_id, author, body, dateposted) if this is any helpful to know.

I tried different ways. Something like this:

SELECT categories.*, entries.*, COUNT(comments.entry_id) AS comm_num 
FROM categories, entries, comments 
WHERE entries.cat_id = categories.cat_id 
AND comments.entry_id = entries.id 
ORDER BY dateposted ASC LIMIT 5;

did not work…

Any help will be greatly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T07:54:59+00:00Added an answer on June 14, 2026 at 7:54 am

    Your first query is essentially a join, and it may not be any faster. You can query for entries (while displaying the corresponding category information) like so:

    SELECT entries.*, categories.* 
    FROM entries
    LEFT JOIN categories ON entries.cat_id = categories.cat_id 
    ORDER BY dateposted ASC 
    LIMIT 5;
    

    Also, it sounds like you don’t actually want to return each comment row in this query, but rather just obtain a comment count per “entry”. For that count, you can likely do this:

    SELECT entries.*, categories.*, COUNT(comments.comment_id) AS comm_num  
    FROM entries
    LEFT JOIN categories on entries.cat_id = categories.cat_id
    LEFT JOIN comments on comments.entry_id = entries.entry_id
    GROUP BY entries.entry_id
    ORDER BY dateposted ASC 
    LIMIT 5;
    

    Note the COUNT function is counting comment IDs, not entry IDs.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is the deal, i'm importing some php files, one of the files has
My PHP app has an import script that can import records. At the moment,
I am trying to create a PHP script that will create a calendar event
I'm creating a PHP script that imports some data from text files into a
I have two PHP services. One that let other users to download a file.
i have a validation class which needs improving. If I require some custom validation
I am importing data from outside into my database MYSQL using PHP Scripts. Encoding
Possible Duplicate: PHP image replacement? I'm writing an edit script that allows a row
I want to grab the User Object in Joomla in another PHP script. Anway,
I am building a PHP CMS from the ground up. There is one super-core

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.