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 140521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T07:37:45+00:00 2026-05-11T07:37:45+00:00

I have a tables called pages , page_views , page_items and page_votes . The

  • 0

I have a tables called pages, page_views, page_items and page_votes. The latter three tables contain a page_id foreign key in order to keep a record of each individual view, item and vote that belongs to a page.

When I query a page, I also want to retrieve COUNT page_views, COUNT page_items and SUM page_votes.vote.

I have pasted a query below. It retrieves the total number of views. I’ve made various attempts to add items and votes to it, but the result is either a syntax error or views/items/votes returned as an identical and ‘wrong’ number, probably due to the way I am joining.

How can I add items and votes to this query?

SELECT   Page.*,   COUNT(*) AS views FROM pages AS Page  INNER JOIN page_views AS PageView   ON Page.id = PageView.page_id  GROUP BY Page.id  ORDER BY views DESC    LIMIT 10 OFFSET 0 
  • 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. 2026-05-11T07:37:45+00:00Added an answer on May 11, 2026 at 7:37 am

    This will select TOP 10 viewed pages, and will count items and votes only for these pages.

    Efficient if you have lots of pages but need only 10 of them, eliminates unneeded counting.

    SELECT  (         SELECT COUNT(*)         FROM   page_views         WHERE  page_views.page_id = pages.id         ) AS views_count,         (         SELECT COUNT(*)         FROM   page_items         WHERE  page_items.page_id = pages.id         ) AS items_count,         COALESCE(         (         SELECT SUM(vote)         FROM   page_votes         WHERE  page_votes.page_id = pages.id         ), 0) AS votes_sum FROM    pages ORDER BY         views_count DESC LIMIT 10 

    Even more efficient query:

    SELECT  pages.*,         (         SELECT COUNT(*)         FROM   page_items         WHERE  page_items.page_id = pages.id         ) AS items_count,         COALESCE(         (         SELECT SUM(vote)         FROM   page_votes         WHERE  page_votes.page_id = pages.id         ), 0) AS votes_sum FROM    (         SELECT  page_id, COUNT(*) AS cnt         FROM    page_views         GROUP BY                 page_id         ORDER BY cnt DESC         LIMIT 10         ) AS pvd,         pages WHERE  pages.id = pvd.page_id 

    , eliminates unneeded joins with pages.

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

Sidebar

Related Questions

In the database we have tables called Sites, Organisations, Person, Department have the same
I have 3 tables called radio, song, and artist controlled by favorite. Now I
I have two tables called A and B . Table A contains request details
Say I have two tables called A (fields: id, phase, name) and B(fields: id,
I have two tables one called fs_note the other called dumy_fs_note I created after
i have two tables one is called addons and holds information about different addons,
I have two tables: servers and stats servers has a column called id that
I have two tables, one called cart and one called items. Now I want
I have two tables Department Professor in which Department has an attribute called HeadID
I have some tables. These tables all have one column in common called 'classified_id':

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.