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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:40:40+00:00 2026-05-27T07:40:40+00:00

n00b questioner here. I’m trying to do a query that checks if the most

  • 0

n00b questioner here. I’m trying to do a query that checks if the most recent activity is within the last 24 hours. I technically can get the result I want, but the inequality in my case statement has to be in the opposite direction as would make sense to me. Here’s my query:

SELECT sqs.registration_id,
       MAX(sqs.completed_at)           AS 'most recent activity',
       DATE_SUB(NOW(), INTERVAL 1 DAY) AS 'one day ago',
       'recent activity?' = CASE
                              WHEN MAX(sqs.completed_at) < 
                                         DATE_SUB(NOW(), INTERVAL 1 DAY) 
                              THEN 1
                              ELSE 0
                            END
FROM   student_quiz_states sqs
WHERE  sqs.score = 100
GROUP  BY sqs.registration_id  

Here’s an example result:

XXXXX   2011-08-02 16:23:53 2011-12-05 00:06:05 0

This user did not have activity in the last 24 hours, so the last value returns 0, as I want it to.

However, that doesn’t make any sense to me. Shouldn’t the case statement return a 1, since the first datetime is much earlier than one day ago? It would make sense to me if my desired results were returned when my when_clause contained a > instead of a <.

Any explanations would be appreciated.

  • 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-27T07:40:40+00:00Added an answer on May 27, 2026 at 7:40 am

    The problem is that your query contains 'recent activity?' = CASE ... END where it should have CASE ... END AS 'recent activity?'. The former is an equality-test rather than an expression with an alias. The reason for the seemingly “inverted” behavior is that, since the CASE expression is numeric (it evaluates to 0 or 1), MySQL performs a numeric equality-test, by converting 'recent activity?' to 0.0 (detailed rules here), such that 'recent activity?' = CASE ... END is true when the CASE expression gives 0 and false when it gives 1. Since MySQL represents true as 1 and false as 0, the end result is the opposite of what you were expecting.

    (Note: An earlier version of this answer, while making the same basic point about equality-tests vs. aliases, and about false and true being 0 and 1, was vague/confused in other respects, since I didn’t recognize that the DBMS was MySQL, and was not aware that some DBMSes allow single-quotes to be used when quoting aliases. So if some of the comments above and below seem a bit strange, it’s because they’re referring to that version. The current state of the answer is thanks in large part to those comments.)

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

Sidebar

Related Questions

n00b here (first Android project). I have been given a custom video codec that
n00b question alert! here is the problem: I am creating a shell script that
OK n00b here with SOAP, Would like some clarification on how to use SOAP.
Serious n00b warning here; please take mercy! So I finished the Nerd Dinner MVC
I'm an email n00b but I am working on an application that sends HTML
I am a N00b here . I print my currency like this : -(IBAction)buttonPressed1:(id)sender
total n00b question here but I can't seem to find the answer searching around
maybe this is a n00b-question. I try to parse an xml-file like that: <?xml
I'm a total C n00b trying to teach myself C off K&R. My question
I am a Clojure n00b trying to create some XML strings. My goal is

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.