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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T12:54:04+00:00 2026-05-11T12:54:04+00:00

I have 1 table filled with articles. For the purpose of this post, lets

  • 0

I have 1 table filled with articles. For the purpose of this post, lets just say it has 4 fields. story_id, story_title, story_numyes, story_numno

Each article can be voted YES or NO. I store every rating in another table, which contains 3 fields: vote_storyid, vote_date (as a timestamp), vote_code (1 = yes, 0 = no).

So when somebody votes yes on an article, it run an update query to story_numyes+1 as well as an insert query to log the story id, date and vote_code in the 2nd table.

I would like to sort articles based on how many YES or NO votes it has. For ‘Best of all time’ rating is obviously simple…. ORDER BY story_numyes DESC.

But how would I go about doing best/worst articles today, this week, this month?

I get the timestamps to mark the cut-off dates for each period via the following:

$yesterday= strtotime('yesterday'); $last_week = strtotime('last week'); $last_month = strtotime('last month'); 

But Im not sure how to utilize these timestamps in a mysql query to achieve the desired results.

  • 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-11T12:54:05+00:00Added an answer on May 11, 2026 at 12:54 pm

    Try something like

    SELECT id,
    SUM(CASE WHEN votedate >= $yesterday THEN 1 ELSE 0 END) AS daycount,
    SUM(CASE WHEN votedate >= $last_week THEN 1 ELSE 0 END) AS weekcount,
    SUM(1) AS monthcount
    FROM votes
    WHERE yes_no = ‘YES’
    AND votedate >= $last_month
    GROUP BY id

    Then make that a subquery and you can get the max values for the counts.

    (Please allowing for the usual syntax sloppiness inherent in an untested query.)


    In response to the comments:

    To use it as an efficient subquery (i.e. not correlated) to get the maximum values:

    SELECT
    MAX(daycount) AS MaxDayCount,
    MAX(weekcount) AS MaxWeekCount,
    MAX(monthcount) AS MaxMonthCount
    FROM
    (
    …. all that stuff …
    ) AS qcounts

    but of course you can’t attribute them to ids, because they are different. If you want them one at a time with ids, you might

    SELECT id, monthcount
    FROM
    (
    …. all that stuff …
    ) AS qcounts
    ORDER BY monthcount DESC
    LIMIT 1

    and do it three times, once for day/week/month.

    Note: this is all to illustrate some things you could accomplish in a single reasonably efficient query. I wouldn’t be surprised if you were to find it’s simplest (and simple == good) to break it up as others suggest.

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

Sidebar

Related Questions

Lets say I have a really big table filled with lots of data (say,
I have a mysql database table filled with 1000+ records, lets say 5000 records.
I have table with some fields that the value will be 1 0. This
I have a table, jos_ezrealty that has address, city, state fields and declat &
I've got a table filled with text input fields (input type=text). I have written
I have a table filled with bus stops. Each stop has a line_id ,
I have a table filled with purchase prices, like this: sku price btw startdate
I have 3 tables: table events . In this I have fields such as
I have a data table filled with text in each table row. How do
I have an HTML table filled with a number of rows. How can I

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.