Select statement with SUM and date range not working

Hi… I am trying to get the SUM total for a specific instructor with an id for records this past week.
So far I have this but no luck with it :slightly_smiling_face:

   SELECT * FROM instructor_time_log, SUM(time_log) AS total WHERE instructor_id = 'SF_52' AND 
  (date > DATE_SUB(NOW(), INTERVAL 1 WEEK));

not working

What exactly does that mean? This is probably the #1 worst way to try and get help on a forum. I would suggest you read this reference.

Well lets start with the basics.
SELECT * FROM instructor_time_log, SUM(time_log) AS total

You’ve mixed up your SELECT and FROM clauses. You’re trying to SELECT the sum, FROM the instructor_time_log table.

3 Likes

This is what worked for me.

SELECT SUM(time_log) AS total FROM instructor_time_log WHERE instructor_id = ? AND date >= (CURDATE() - INTERVAL 1 WEEK)

1 Like

I’m surprised that isn’t giving some type of “reserved word” error?

Technically, date isnt a reserved keyword, it’s just a keyword. (at least, it isnt in MySQL, which is what I assume from the structure and functions used is what we’re talking about here. YMMV in other engines.)

Using it is still a bad idea, but it won’t cause a failure.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.