gpt4 book ai didi

mysql - 从任一表中选择值

转载 作者:行者123 更新时间:2023-11-29 23:50:17 24 4
gpt4 key购买 nike

我使用 mySQL,并尝试查找一个表或另一个表中的行。以下是表及其字段:

user: id(PK), email, password, is_teacher

teacher: id, no_teacher(PK), name, id_group, no_class
teacher_information:no_teacher, cell_phone, SSN, email

student: id, no_student(PK), name, id_group, no_class
student_information:cell_phone, email, grade, no_student

user_role: id, id_role(PK)

group_leader: id_group, id_role
group_member: id_group, id_role

group: id_group(PK), name

每个用户(教师或学生)都可以是组中的领导者或成员(不能同时是两者),并且任何用户都可以属于不同的组。我想要列出参与特定组的每个人的姓名和电子邮件。任何人都可以帮我获得这种情况下的sql查询吗?

最佳答案

如果将字段teacher_id添加到teacher_information表,并将student_id添加到student_information字段

SELECT
*
FROM
(SELECT
t.name as `name`,
ti.email as `email`,
g.name as `group_name`,
g.id_group as `id_group`
FROM
teacher t,
teacher_information ti,
group g
WHERE
t.id = ti.teacher_id and
g.id_group = t.id_group
UNION
SELECT
s.name as `name`,
si.email as `email`,
g.name as `group_name`,
g.id_group as `id_group`
FROM
student s,
student_information si,
group g
WHERE
s.id = si.student_id and
g.id_group = s.id_group
) as a
where
a.id_group = ?
;

关于mysql - 从任一表中选择值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25689592/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com