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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:32:40+00:00 2026-06-17T09:32:40+00:00

I have three tables with these fields: product_views : ProductMasterID, Date product_clicks : ProductMasterID,

  • 0

I have three tables with these fields:

product_views: ProductMasterID, Date

product_clicks: ProductMasterID, Date

product_master: ProductMasterID, Category

Anytime someone views a product on my site, it adds a row to product_views.
Anytime someone clicks on a product on my site, it adds a row to product_clicks

I am trying to write a SQL query (in MySQL/PHP) that will show me the popularity of products. Popularity is how many times it was viewed and clicked added together. I want to be able to also choose a date range, and within a certain category.

I can make it work without the date/category constrictions. I am now trying to add in the date range. This is what I have so far, for popularity of the last week:

SELECT *, COUNT(*) AS Popularity FROM 

    (SELECT product_views.ProductMasterID, product_views.Date 
         FROM product_views 
         WHERE product_views.Date BETWEEN '".date('Y-m-d',strtotime('now'))."' 
            AND '".date('Y-m-d',strtotime('-7days'))."'"."

UNION ALL 

    SELECT product_clicks.ProductMasterID,product_clicks.Date 
        FROM product_clicks 
        WHERE product_clicks.Date BETWEEN '".date('Y-m-d',strtotime('now'))."' 
           AND '".date('Y-m-d',strtotime('-7days'))."'".") a 

GROUP BY ProductMasterID ORDER BY Popularity DESC LIMIT 100"

Except that it doesnt work, it produces no results. However, if I delete either one of the WHERE clauses, then it does work for some reason, but I need both of them. So my questions are:

  1. How to make it work with both WHERE clauses
  2. How to do the category part e.g. selecting only products which have a category of ‘Food’. I think I would have to do a join somewhere with the product_master table, but looks tricky.

Any help would be greatly appreciated.

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-06-17T09:32:41+00:00Added an answer on June 17, 2026 at 9:32 am

    If you want to use a JOIN, here is a solution:

    • SQLFIDDLE DEMO

    Query:

    SELECT M.PMID, M.CATEGORY, 
    (COALESCE(V.VCNT,0) +
    COALESCE(C.CCNT,0)) AS TOTAL_COUNT
    FROM Master M
    LEFT JOIN (SELECT PMID, COUNT(*) AS VCNT
               FROM VIEWS
               WHERE DATE between 
               DATE_ADD(NOW(), INTERVAL - 7 DAY)
               and now()
               GROUP BY PMID) V
    ON V.PMID = M.PMID
    LEFT JOIN (SELECT PMID, COUNT(*) AS CCNT
               FROM CLICKS
               WHERE DATE between 
               DATE_ADD(NOW(), INTERVAL - 7 DAY)
               and now()
               GROUP BY PMID) C
    ON C.PMID = M.PMID
    GROUP BY M.PMID
    ;
    

    Results:

    | PMID | CATEGORY | TOTAL_COUNT |
    ---------------------------------
    |    1 |    Shoes |           6 |
    |    2 |    Books |           4 |
    |    3 |     Food |           3 |
    

    Using the UNION and just for the SQL part:

    SELECT ID, COUNT(*) AS Popularity FROM 
    (
        (SELECT product_views.ProductMasterID AS ID, count(*) 
             FROM product_views 
             WHERE product_views.Date BETWEEN '".date('Y-m-d',strtotime('now'))."' 
                AND '".date('Y-m-d',strtotime('-7days'))."'"."
             GROUP BY product_views.ProductMasterID)
    
    UNION ALL 
    
        (SELECT product_clicks.ProductMasterID AS ID, COUNT(*)
            FROM product_clicks 
            WHERE product_clicks.Date BETWEEN '".date('Y-m-d',strtotime('now'))."' 
               AND '".date('Y-m-d',strtotime('-7days'))."'".") a
         GROUP BY product_clicks.ProductMasterID)
    
    GROUP BY ProductMasterID 
    ORDER BY Popularity DESC LIMIT 100"
    

    How about this?

    SELECT M.PMID, M.CATEGORY, COUNT(V.PMID), COUNT(C.PMID)
    FROM MASTER M
    LEFT JOIN VIEWS V
    ON V.PMID = M.PMID
    LEFT JOIN CLICKS C
    ON C.PMID = M.PMID
    WHERE V.DATE BETWEEN NOW() AND NOW() INTERVAL - 7 DAY
    AND C.DATE BETWEEN NOW() AND NOW() INTERVAL 7 DAY
    GROUP BY M.PMID, V.DATE, C.DATE
    ;
    

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

Sidebar

Related Questions

I have a Doctrine2 project with three tables: Product , Category and Rating .
I have three tables. Product, product categories and product category links. The links table
I have three tables (lots in reality but these three are the ones I
Say I have these three tables: Table: Baskets id | name 1 Sale 2
I have users table. There are three other tables: developers, managers, testers. All of
I have two tables Product having following structure: ProductID,ProductName, IsSaleTypeA, IsSaleTypeB, IsSaleTypeC 1, AAA,
I have three tables, a parent and two children, the two children have FK
I have three tables, machines holding vending machines, products holding all possible products, and
I have two tables that I'm querying from. In one table, there are fields
I have two MySQL tables: product has id , title , price and some

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.