今天自己写的一条SQL语句,用到了表连接和临时表,语法供自己以后参考。
SELECT us.school_name 学校名称, CONCAT( ug.grade_name, uc.classes_name ) 年级班级, uu.user_id 用户ID, uu.user_real_name 学生姓名, ta.assignment_id 作业序号, ta.assignment_write_time 作文写作耗时(以秒记), ta.assignment_word_count 字数, ta.assignment_submit_count 作文提交次数, ta.assignment_start_date 布置作业开始时间, ta.assignment_end_date 作业截止时间, ta.assignment_submit_date 作业提交教师时间, ta.assignment_mark1 作业评分的第一项(内容), ta.assignment_mark2 作业评分的第二项(语言), ta.assignment_mark3 作业评分的第三项(文采或结构), ta.assignment_mark_sum 三项评分的总分, teacher_list.user_real_name 教师姓名 FROM t_assignment ta LEFT JOIN u_user uu ON ta.user_id = uu.user_id LEFT JOIN u_classes_user ucu ON uu.user_id = ucu.user_id LEFT JOIN u_classes uc ON ucu.classes_id = uc.classes_id LEFT JOIN u_school_grade usg ON uc.school_grade_id = usg.school_grade_id LEFT JOIN u_grade ug ON usg.grade_id = ug.grade_id LEFT JOIN u_school us ON usg.school_id = us.school_id LEFT JOIN u_user_role uur ON uu.user_id = uur.user_id LEFT JOIN u_role ur ON uur.role_id = ur.role_id LEFT JOIN ( SELECT ucu.classes_id, uu.user_real_name FROM u_user uu LEFT JOIN u_user_role uur ON uu.user_id = uur.user_id LEFT JOIN u_role ur ON uur.role_id = ur.role_id LEFT JOIN u_classes_user ucu ON uu.user_id = ucu.user_id WHERE uur.role_id = '42' ) teacher_list ON teacher_list.classes_id = ucu.classes_id WHERE ta.assignment_start_date > '2014-09-01 00:00:00' AND ta.assignment_start_date < '2015-01-31' AND uur.role_id = '38' AND us.school_id IN ('7', '8', '5', '2');