【问题标题】:Find entry which has same has many along with its details查找具有相同内容的条目及其详细信息
【发布时间】:2014-03-29 10:53:39
【问题描述】:

我需要帮助来编写一个查询,该查询会提取一个具有相同内容的条目。老师可以选择任何科目来教授,学生可以选择任何科目来教授。需要找到一位选择教授学生选择的相同科目组合的老师。

表学生

id name
1  x
2  y

表 student_Subjects

id subject_id student_id
1  1          1
2  2          1
3  1          2

老师

id name
1  tx
2  ty

表teacher_subjects

id subject_id teacher_id
1  1          2
2  2          2
3  1          1

主题

id name
1  English
2  Maths

现在需要找到一位选择与学生 x 教授相同科目的老师以及学生和老师的姓名。

我问过类似的问题,结果没有学生姓名和老师姓名。 这是查询:

 SELECT GROUP_CONCAT(subject_id ORDER BY subject_id) as teacher_concat ,teacher_id
       FROM teacher_subjects 
       GROUP BY teacher_id 
       HAVING teacher_concat IN
                    (SELECT group_concat(subject_id ORDER BY subject_id)
                     FROM student_subjects GROUP BY student_id)

【问题讨论】:

    标签: mysql sql


    【解决方案1】:

    我认为这应该可行:

    select
            teacher.id, 
            teacher.name,
            student.id,
            student.name
    from
            teacher,
            student,
            (select
                    teacher_id,
                    GROUP_CONCAT(
                            distinct subject_id
                            order by subject_id
                            separator ' '
                    ) x
            from
                    teacher_subjects
            group by
                    teacher_id
            ) t,
            (select
                    student_id
                    GROUP_CONCAT(
                            distinct subject_id
                            order by subject_id
                            separator ' '
                    ) x
            from
                    student_subjects
            group by
                    student_id
            ) s
    where
            t.x=s.x and
            t.teacher_id=teacher.teacher_id and
            s.student_id=student.student_id
    

    【讨论】:

    • 给出错误:您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以获取正确的语法,以便在第 13 行 @Lajos 的“group by subject_id ) m from teacher_sub”附近使用
    • 谢谢。我修复了两件事,希望对您有所帮助。
    猜你喜欢
    • 2012-05-28
    • 2019-04-23
    • 1970-01-01
    • 1970-01-01
    • 2014-01-26
    • 1970-01-01
    • 2015-08-13
    • 2011-12-04
    • 2015-11-02
    相关资源
    最近更新 更多