Last active
November 3, 2020 04:47
-
-
Save sherwinmartin/84cec8e1a795467cffddbb49bfa081e1 to your computer and use it in GitHub Desktop.
sample database for help
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE SCHEMA `mydb`; | |
USE `mydb`; | |
/** | |
* Student records | |
*/ | |
CREATE TABLE `students` ( | |
`id` INT AUTO_INCREMENT PRIMARY KEY, | |
`name` VARCHAR(100) NOT NULL | |
) ENGINE=INNODB; | |
INSERT INTO `students` VALUES (1, 'Mickey Mouse'); | |
INSERT INTO `students` VALUES (2, 'Donald Duck'); | |
INSERT INTO `students` VALUES (3, 'Goofy'); | |
/** | |
* Enrollment records | |
*/ | |
CREATE TABLE `enrollments` ( | |
`id` INT AUTO_INCREMENT PRIMARY KEY, | |
`student_id` INT, | |
`start_date` DATE NOT NULL, | |
`end_date` DATE NOT NULL, | |
FOREIGN KEY (`student_id`) | |
REFERENCES `students` (`id`) | |
) ENGINE=INNODB; | |
INSERT INTO `enrollments` VALUES (1, 1, '2020-01-01', '2020-01-31'); | |
INSERT INTO `enrollments` VALUES (2, 2, '2020-01-01', '2020-01-14'); | |
INSERT INTO `enrollments` VALUES (3, 3, '2020-02-01', '2020-02-14'); | |
/** | |
* Attendance records | |
*/ | |
CREATE TABLE `attendances` ( | |
`id` INT AUTO_INCREMENT PRIMARY KEY, | |
`enrollment_id` INT, | |
`attendance_date` DATE NOT NULL, | |
`has_attended` CHAR(1), | |
FOREIGN KEY (`enrollment_id`) | |
REFERENCES `enrollments` (`id`) | |
) ENGINE=INNODB; | |
/** | |
* Clean up | |
*/ | |
DROP SCHEMA `mydb`; | |
/** | |
* Question...? | |
How can I create attendance records based on dates that fall between enrollments.start_date and enrollments.end_date? For example. Mickey Mouse has an enrollment record that begins January 1, 2020 to January 31, 2020. There should be 30 attendance records created - one for each day between the dates. | |
I played around with recursive CTE (common table extension) https://www.mysqltutorial.org/mysql-recursive-cte. But I can't wrap my head around it. | |
Also started doing stored procedure where I would select the enrollment records, loop through it and each pass do an insert all the records between the dates... probably using cursor. But before I go any further, I'd like to see how other people would approach it. Thanks. | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment