账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    mysql多个表联合排序
    36
    0

    开发过程中遇到个mysql的问题,我有多个表,多个表之间有联系,每个表都有单独的排序。
    course > course_level > course_unit > course_lesson,这四个表的层级关系就是课程下面很多level,每个level下面有多个unit,每个unit下面有多个lesson,所有表的排序字段均为sort,全部是按照升序排列,如果某个级别sort相同,就按照id升序排列,例如level表里面id=5的sort是0,id=10的sor也是0,那么id5就排在前,每个子级别下的sort互不干扰,例如,course_unit表里面的course_level_id=2,和course_level_id=5的unit都会出现sort=0,sort=5的情况,意思就是下级的排序和上级的排序相关联。

    现在我需要根据course,course_level,course_unit的顺序来把course_lesson的顺序确定下来,就是整个课程体系的id排序,请问这个mysql语句怎么写?lesson_id大概有1000个,只需要排好序的course_lesson表里面的id字段。

    #课程分类表
    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 '该Level下总unit数',
      `lesson_num` smallint(5) unsigned NOT NULL DEFAULT '0' COMMENT '该level下总lesson数',
      `lesson_class_hour` int(8) unsigned NOT NULL DEFAULT '0' COMMENT '该Level下所有Lesson总学时(分钟)',
      `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='课程信息表';
    
    #课程等级表
    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 '该Level下总unit数',
      `lesson_num` smallint(5) unsigned NOT NULL DEFAULT '0' COMMENT '该level下总lesson数',
      `lesson_class_hour` int(8) unsigned NOT NULL DEFAULT '0' COMMENT '该Level下所有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=15 DEFAULT CHARSET=utf8 COMMENT='课程下Level信息表';
    
    #单元表
    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='课程下单元信息表';
    
    #课程表
    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信息表';
    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 浅唱_↘幸諨 普通会员 1楼

      在MySQL中,你可以使用UNION操作符来联合多个表并进行排序。以下是一个基本的示例:

      假设你有两个表,一个是employees,一个是departments。你想根据员工的部门名称对员工进行排序。

      首先,你可以使用UNION操作符将这两个表联合起来:

      sql SELECT * FROM employees UNION ALL SELECT * FROM departments;

      然后,你可以使用ORDER BY子句对联合后的结果进行排序。假设你想要按照部门名称升序排序,你可以这样写:

      sql SELECT * FROM employees UNION ALL SELECT * FROM departments ORDER BY departments.department_name ASC;

      这个查询将返回employees表和departments表的联合结果,并按照部门名称升序排序。

      请注意,UNION ALL操作符会将两个结果集中的所有行合并成一个结果集。如果你只想保留一个结果集中的所有行,你可以使用UNION操作符和DISTINCT关键字:

      sql SELECT DISTINCT * FROM employees UNION ALL SELECT DISTINCT * FROM departments;

      这将返回employees表和departments表的联合结果,并且只保留每个部门中的一个行。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部