Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

I have a table names 'scoredata' having fields id, lessonid, levelid, studentid, addedate in which score for each day will be entered by the admin. I want to select only those studentid whose score has not been added from last four weeks. How can I do this in a single query.

share|improve this question
add comment

1 Answer 1

SELECT studentid FROM
(
    SELECT MAX(addedate) lastdate,studentid
    FROM scoredata GROUP BY addedate
) A
WHERE lastdate < ( DATE(NOW()) - INTERVAL 4 WEEK );

Query Breakdown

  • The subquery gets the last date each student entered a score
  • The WHERE clause outside the subquery checks to all lastdate values older than 4 weeks

Make sure you index addedate. If you didn't then run

ALTER TABLE scoredata ADD INDEX (addedate);

Give it a Try !!!

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.