SELECT team_with.participant1,team_with.participant2,team_with.participant3
FROM event,team_with
WHERE team_with.for_event_no=event.event_no AND
event.event_no=4 AND
team_with.participant1=9 OR
team_with.participant2=9 OR
team_with.participant3=9;
我已经编写了特定的查询,并连续获得了所需的ID。我无法修改此查询,以致于无法显示这些ID,而是显示连接到该ID的名称。
该student_detatil
表由PK(sam_id)
和属性组成name
。
当前查询显示的ID是连接到的FK student_detail.sam_id.
。
乘以存储不同参与者的列似乎是一个错误的设计。考虑为每个参与者创建一个单独的行,并将其存储在表中。您的加入逻辑也将更加容易。
另外,请使用显式JOIN
语法-通过将联接逻辑与数据检索条件分开,可以使查询更清晰,更易于理解。
请记住,运算符AND
的优先级高于OR
,因此您的event.event_no = 4
不适用于每个参与者条件。我相信这是一个错误,但是您是一个要判断的人。
至于查询本身,您可以将OR
条件应用到联接中,或者简单地将student_detail表联接三次。
SELECT
s1.name,
s2.name,
s3.name
FROM
event e
INNER JOIN team_with t ON t.for_event_no = e.event_no
LEFT JOIN student_detail s1 ON s1.sam_id = t.participant1
LEFT JOIN student_detail s2 ON s2.sam_id = t.participant2
LEFT JOIN student_detail s3 ON s3.sam_id = t.participant3
WHERE
e.event_no = 4
AND ( t.participant1=9 OR t.participant2=9 OR t.participant3=9 );
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句