guoyu/query_student_206.sql
2025-12-11 23:28:07 +08:00

55 lines
1.4 KiB
SQL
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

-- ========================================
-- 查询编号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;