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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T21:15:04+00:00 2026-06-07T21:15:04+00:00

I have two mysql tables and i want to do a select query: My

  • 0

I have two mysql tables and i want to do a select query: My objective is to calculate for a certain subscriber his points for the last seven days starting from the current time .
The first table presents for each subscriber the time he hits on a certain content from content id 1 2 or 3.
He could for a certain day have no content hits so he will not have points for this day.

enter image description here

enter image description here

The tables are shown in these pictures.
MY select query which is incomplete :

   SELECT SUM( points ) AS POINT, DAY FROM
 (SELECT content_hits.content_id, COUNT( * ) * points AS points, 
DATE_FORMAT( hit_time,  "%d-%m-%Y" ) AS  DAY 
FROM content_hits JOIN content 
WHERE content_hits.content_id = content.content_id AND subscriber_id =1
 AND DATE_FORMAT( hit_time,  "%Y-%m-%d" ) > ( CURDATE( ) - INTERVAL 7 DAY )
 GROUP BY content_hits.content_id, DAY) AS tm 
GROUP BY DAY 
ORDER BY DAY DESC

The result is shown in this picture:
enter image description here

I expects having a result with zero count for the days: 17-7-2102 16-7-2012 15-7-2012 14-7-2012 and 12-7-2012.
Any suggestion?

  • 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-07T21:15:06+00:00Added an answer on June 7, 2026 at 9:15 pm

    Need to create calendar table for use in left join

    CREATE TABLE ints (i INTEGER);
    INSERT INTO ints VALUES (0), (1), (2), (3), (4), (5), (6), (7), (8), (9);
    

    Left join the calendar table to your query.

     SELECT cal.date,COALESCE(a.point,0) as point
     FROM (
        SELECT DATE_FORMAT(CURDATE() + INTERVAL a.i * 10 - b.i DAY,"%Y-%m-%d") as date
        FROM ints a JOIN ints b
        ORDER BY a.i * 10 - b.i
     ) cal LEFT JOIN 
     (SELECT SUM( points ) AS POINT, DAY FROM
      (SELECT content_hits.content_id, COUNT( * ) * points AS points, 
        DATE_FORMAT( hit_time,  "%Y-%m-%d" ) AS  DAY 
       FROM content_hits JOIN content 
       WHERE content_hits.content_id = content.content_id AND subscriber_id =1
         AND DATE_FORMAT( hit_time,  "%Y-%m-%d" ) > ( CURDATE( ) - INTERVAL 7 DAY )
       GROUP BY content_hits.content_id, DAY) AS tm 
       GROUP BY DAY 
     )a
     ON cal.date = a.day
     WHERE cal.date BETWEEN CURDATE( ) - INTERVAL 7 DAY AND CURDATE()
     ORDER BY cal.date desc;
    

    SQL DEMO here.

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

Sidebar

Related Questions

I have two mysql tables and i want to merge the results of these
I have two MySQL tables, and I want to find and replace text strings
I have two tables in my MySQL database (table1 and table 2). I want
I have two tables in mySQL, notably accounts and subscriber_data. I want to use
I have two tables in MySql Company : (cname,city) works : (ename,cname,salary) I want
I have a two mysql tables that I want to merge together in one
I have two MySQL tables something like this: tool_owners: tool_owners_id | user_id | ...
I have two MySQL tables, j and t, with a third normalising table, jt.
I have two MySQL tables structed as follows; advert +-------------------+--------------+ | Field | Type
I have two MySQL tables: attributes (attributeid, name) productsattributes (productid, attributeid, displayvalue) The required

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.