I encountered a mysql problem during development. I have multiple tables, there are relationships between multiple tables, and each table has a separate sort. The hierarchical relationship of the four tables
course > course_level > course_unit > course_lesson, is that under many level, under the course, there are multiple unit, under each level and multiple lesson, under each unit. The sorting fields of all tables are all sort, in ascending order. If a certain level of sort is the same, it is arranged in ascending order of id. For example, in the level table, if the sort of id=5 is 0 and sor of 10 is also 0, then id5 is at the top of the list. The sort under each sub-level does not interfere with each other. For example, the course_level_id=2, in the course_unit table and the unit of course_level_id=5 will occur in the case of sort=0,sort=5, which means that the sorting of the subordinate is associated with the sorting of the superior.
now I need to determine the order of course,course_level,course_unit according to the order of course_lesson, that is, the id sort of the whole course system. How do I write this mysql statement? There are about 1000 lesson_id, and all you need is the id field in the sorted course_lesson table.
-sharp
DROP TABLE IF EXISTS `course`;
CREATE TABLE `course` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`course_type` tinyint(3) unsigned NOT NULL DEFAULT "0" COMMENT "(1.;2.;)",
`lesson_type` tinyint(3) NOT NULL DEFAULT "0" COMMENT "(0.;1.;2.;3.;4.;5.)",
`name` varchar(64) NOT NULL DEFAULT "" COMMENT "",
`level_num` tinyint(3) unsigned NOT NULL DEFAULT "0" COMMENT "",
`unit_num` tinyint(3) unsigned NOT NULL DEFAULT "0" COMMENT "Levelunit",
`lesson_num` smallint(5) unsigned NOT NULL DEFAULT "0" COMMENT "levellesson",
`lesson_class_hour` int(8) unsigned NOT NULL DEFAULT "0" COMMENT "LevelLesson()",
`class_hour` int(8) DEFAULT "25" COMMENT "",
`lesson_before_time` int(8) DEFAULT "5" COMMENT "()",
`lesson_after_time` int(8) DEFAULT "5" COMMENT "()",
`lesson_interval_time` int(8) DEFAULT "15" COMMENT "()",
`sort` tinyint(3) unsigned NOT NULL DEFAULT "0" COMMENT ",",
`status` tinyint(3) unsigned NOT NULL DEFAULT "1" COMMENT "(1.;2.;3.;4.)",
`creator` int(11) unsigned NOT NULL,
`updator` int(11) unsigned NOT NULL DEFAULT "0",
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
`teacher_absence_time` int(8) DEFAULT "15" COMMENT "",
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 COMMENT="";
-sharp
DROP TABLE IF EXISTS `course_level`;
CREATE TABLE `course_level` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT "LevelId",
`course_id` int(11) unsigned NOT NULL DEFAULT "0" COMMENT "Id",
`name` varchar(64) NOT NULL DEFAULT "" COMMENT "level,level1~level6,",
`title` varchar(64) NOT NULL DEFAULT "" COMMENT "Level",
`unit_num` tinyint(3) unsigned NOT NULL DEFAULT "0" COMMENT "Levelunit",
`lesson_num` smallint(5) unsigned NOT NULL DEFAULT "0" COMMENT "levellesson",
`lesson_class_hour` int(8) unsigned NOT NULL DEFAULT "0" COMMENT "LevelLesson()",
`sort` tinyint(3) unsigned NOT NULL DEFAULT "1" COMMENT ",",
`status` tinyint(3) unsigned NOT NULL DEFAULT "1" COMMENT "Level(1.;2.;3.;4.)",
`creator` int(11) unsigned NOT NULL DEFAULT "0" COMMENT "",
`updator` int(11) unsigned NOT NULL DEFAULT "0" COMMENT "",
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=utf8 COMMENT="Level";
-sharp
DROP TABLE IF EXISTS `course_unit`;
CREATE TABLE `course_unit` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT "LevelId",
`course_id` int(11) unsigned NOT NULL DEFAULT "0" COMMENT "courseId",
`course_level_id` int(11) unsigned NOT NULL DEFAULT "0" COMMENT "Id",
`name` varchar(64) NOT NULL DEFAULT "" COMMENT "",
`title` varchar(64) NOT NULL DEFAULT "" COMMENT "",
`lesson_num` smallint(5) unsigned NOT NULL DEFAULT "0" COMMENT "lesson",
`lesson_class_hour` int(8) unsigned NOT NULL DEFAULT "0" COMMENT "lesson()",
`sort` tinyint(3) unsigned NOT NULL DEFAULT "1" COMMENT ",",
`status` tinyint(3) unsigned NOT NULL DEFAULT "1" COMMENT "Level(1.;2.;3.;4.)",
`creator` int(11) unsigned NOT NULL DEFAULT "0" COMMENT "",
`updator` int(11) unsigned NOT NULL DEFAULT "0" COMMENT "",
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=138 DEFAULT CHARSET=utf8 COMMENT="";
-sharp
DROP TABLE IF EXISTS `course_lesson`;
CREATE TABLE `course_lesson` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT "LevelId",
`course_id` int(11) unsigned NOT NULL,
`course_level_id` int(11) unsigned NOT NULL,
`course_unit_id` int(11) unsigned NOT NULL DEFAULT "0" COMMENT "Id",
`courseware_id` int(11) NOT NULL DEFAULT "0" COMMENT "ID",
`preview_video_id` tinyint(3) NOT NULL DEFAULT "0" COMMENT "",
`reference_video_id` tinyint(3) NOT NULL DEFAULT "0" COMMENT "id",
`name` varchar(64) NOT NULL DEFAULT "" COMMENT "level,level1~level6,",
`title` varchar(64) NOT NULL DEFAULT "" COMMENT "",
`topic` varchar(64) NOT NULL DEFAULT "" COMMENT "",
`subject` text NOT NULL COMMENT "",
`class_hour` tinyint(3) unsigned NOT NULL DEFAULT "0" COMMENT "()",
`sort` tinyint(3) unsigned NOT NULL DEFAULT "1" COMMENT ",",
`status` tinyint(3) unsigned NOT NULL DEFAULT "1" COMMENT "Level(1.;2.;3.;4.)",
`creator` int(11) unsigned NOT NULL DEFAULT "0" COMMENT "",
`updator` int(11) unsigned NOT NULL DEFAULT "0" COMMENT "",
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=763 DEFAULT CHARSET=utf8 COMMENT="Lesson";