I have a table of classes, and a table of subjects.
CLASS |class_id|class_name|subject_id|date_time| ------------------------------------------ (imagine some rows here) SUBJECT |subject_id|subject_name|current_class_count| --------------------------------------------- (imagine some rows here)
For indexing purposes I’d like to update the list of subjects every hour with the list of classes which have a date_time greater than right now.
I can do a select statement like this:
SELECT count(*) AS num, subject_id FROM class GROUP BY subject_id where date_time > NOW()
and I will get something like
RESULT |num|subject_id| ---------------- | 8 | 1 | | 6 | 2 | | 9 | 3 | ----------------
What’s the most efficient way to get the subject table updated with the current_class_count? I could do this with PHP by looping through and doing several update statements, but I think mySql should have an easier way.
Edit: How about this:
As long as I’ve typed it right, basically you should be able to run this once an hour and it will update your SUBJECT table.
Joining a table in an UPDATE statement is a bit different in MySQL compared to Microsoft SQL. Here is a link about it:
http://blog.ookamikun.com/2008/03/mysql-update-with-join.html