I've been struggling to come up with a solution to the following problem.
I have two tables with the following structure.
member table:
╔═══════════╦═════════════╦═════════╗
║ member_no ║ member_type ║ team_no ║
╠═══════════╬═════════════╬═════════╣
║ CON123 ║ monthly ║ 12 ║
╠═══════════╬═════════════╬═════════╣
║ CON456 ║ monthly ║ 13 ║
╠═══════════╬═════════════╬═════════╣
║ CON789 ║ annual ║ 13 ║
╚═══════════╩═════════════╩═════════╝
team table:
╔═════════╦════════════╦══════════════╗
║ team_no ║ supervisor ║ member_count ║
╠═════════╬════════════╬══════════════╣
║ 12 ║ John ║ 1 ║
╠═════════╬════════════╬══════════════╣
║ 13 ║ Joe ║ 2 ║
╠═════════╬════════════╬══════════════╣
║ 14 ║ Allan ║ 0 ║
╚═════════╩════════════╩══════════════╝
What I would like to do is something like this when inserting values into the "team" table:
INSERT INTO team (team_no , supervisor , member_count)
VALUES ("13" , "Joe" , SELECT COUNT(team_no) FROM member WHERE team_no = "13");
Essentially, I would like the member_count column from the "team" table to be populated with the total number of members belonging to that team using the COUNT function on the member table.
I'd be really thankful if anyone can help me with this problem.