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

  • Home
  • SEARCH
  • 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 6541773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:08:45+00:00 2026-05-25T11:08:45+00:00

I think it is easier if I explain what I am trying to do.

  • 0

I think it is easier if I explain what I am trying to do.

Say I have 2 tables, one is pages and the other is views.

Each page has a unique ID, when the page is visited, a record is entered in to the views.

Say I wanted to list the pages and the total views of the page together.

So I would have a query which selects the pages from the database and uses a loop to display them, but I want to also find out the total views it has and display that number next to the page name.

How would I achieve this?

When I run a query inside a fetch loop, I get the following error:

Commands out of sync; you can't run this command now

Thanks.

  • 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-05-25T11:08:46+00:00Added an answer on May 25, 2026 at 11:08 am

    Use a join, something like this:

    SELECT page.*, view.*
    FROM page
    LEFT OUTER JOIN view
    ON view.page_id = page.id
    

    By using LEFT OUTER JOIN, pages without views will still be returned, but the columns from view will be NULL.

    This will get you all of the results you need in one single query. It’s usually better to have the database engine do the work of correlating tables together than to do so on your own. Then you’re only making one trip to the database engine, and it can perform the correlation much faster than your code can.


    To get the count, you can either do a join-with-group-by (which may be an ugly query, depending on your DB engine) or a query with a subquery:

    SELECT
        page.*,
        (
            SELECT COUNT(view.id)
            FROM view
            WHERE view.page_id = page.id
        ) AS view_count
    FROM page
    

    The group-by variant would look something like this:

    SELECT page.*, COUNT(view.id)
    FROM page
    LEFT OUTER JOIN view
    ON view.page_id = page.id
    GROUP BY page.id
    

    Depending on your DB engine and schema, you may have to group by more than page.id.

    A decent query planner will turn the subquery example into a join, so the performance of the two queries should be similar, if not identical. Nevertheless, you should use EXPLAIN to determine if one variant gets optimized better than the other.

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

Sidebar

Related Questions

I think it's easier to explain my problem with an example. I have one
I think that it's easier to explain my problem with an example: Based on
I think this will be easier to show you than explain, but basically I
I think, it is easier explain using an example. Let's take a class that
i think my whole questions is inside title bar =] but i'll explain little
OK, going to try and explain this properly as I think thats why an
This one I really think is not possible to solve through XSLT, so that
I am trying to create a small server type application and have a question
I am trying to learn about unit testing in Zend Framework. I have installed
I have been trying to strip out some data from HTML files. I have

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.