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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:31:52+00:00 2026-05-23T00:31:52+00:00

I have a table of classes, and a table of subjects. CLASS |class_id|class_name|subject_id|date_time| ——————————————

  • 0

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.

  • 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-23T00:31:52+00:00Added an answer on May 23, 2026 at 12:31 am

    Edit: How about this:

    UPDATE SUBJECT
    LEFT JOIN (
    SELECT count(*) AS num, subject_id
    FROM class
    GROUP BY subject_id
    where date_time > NOW()) AS t ON SUBJECT.subject_id = t.subject_id
    SET SUBJECT.current_class_count = coalesce( t.num, 0 )
    

    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

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

Sidebar

Related Questions

I have 2 classes: [Table(Name = _Reference2)] public class Customer { [Column(Name = _IDRRef)]
I have two classes. In my first class i have table view which contains
I have implemented inheritance for two parent classes called Table and Field . Each
I have two classes generated by LINQ2SQL both from the same table so they
I have two classes with many-to-many relation so I created a join table in-between
I have 4 classes as below... @MappedSuperclass @Inheritance(strategy=InheritanceType.TABLE_PER_CLASS) public abstract class Platform{ @Id @Column(name
I have 3 classes. A Waiter (thread), a Table (thread), and a Queue (not
Hi every one I have these classe @Entity @Table(name = login, uniqueConstraints={@UniqueConstraint(columnNames={username_fk})}) public class
I have a table called Classes which stores information on College classes. It has
I have some set of problem in search. I have a table classDetails and

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.