-- ========================================
-- 查询编号206学生的班级数据
-- ========================================

-- 查询1：查看编号206的所有班级记录
SELECT 
    sc.id,
    sc.student_id,
    sc.class_id,
    c.class_name,
    sc.status,
    sc.join_time,
    sc.create_time
FROM student_class sc
LEFT JOIN class c ON sc.class_id = c.id
WHERE sc.student_id = 206
ORDER BY sc.join_time DESC, sc.create_time DESC;

-- 查询2：查看所有status=1（活跃）的班级
SELECT 
    sc.id,
    sc.student_id,
    sc.class_id,
    c.class_name,
    sc.status,
    sc.join_time
FROM student_class sc
LEFT JOIN class c ON sc.class_id = c.id
WHERE sc.student_id = 206 AND sc.status = 1
ORDER BY sc.join_time DESC, sc.create_time DESC;

-- 查询3：查看编号206在课程分配表中的数据
SELECT 
    ca.id,
    ca.course_id,
    ca.student_id,
    ca.class_id,
    co.course_name,
    cl.class_name
FROM course_assignment ca
LEFT JOIN course co ON ca.course_id = co.id
LEFT JOIN class cl ON ca.class_id = cl.id
WHERE ca.student_id = 206
ORDER BY ca.create_time DESC;

-- 查询4：查看所有有多个班级的学生
SELECT 
    student_id,
    COUNT(*) as class_count,
    GROUP_CONCAT(CONCAT(class_id, '(', (SELECT class_name FROM class WHERE id = class_id), ')') SEPARATOR ', ') as classes
FROM student_class
WHERE status = 1
GROUP BY student_id
HAVING COUNT(*) > 1;
