[英]SQL comparing two tables with common id but the id in table 2 could being in two different columns
Given the following SQL tables: 给定以下SQL表:
Administrators: 管理员:
id Name rating
1 Jeff 48
2 Albert 55
3 Ken 35
4 France 56
5 Samantha 52
6 Jeff 50
Meetings: 会议:
id originatorid Assitantid
1 3 5
2 6 3
3 1 2
4 6 4
I would like to generate a table from Ken's point of view (id=3) therefore his id could be possibly present in two different columns in the meetings' table. 我想从Ken的角度生成一个表(id = 3),因此他的id可能出现在会议表的两个不同列中。 (The statement IN does not work since I introduce two different field columns).
(因为我引入了两个不同的字段列,所以IN语句不起作用)。
Thus the ouput would be: 因此输出将是:
id originatorid Assitantid
1 3 5
2 6 3
If you really just need to see which column Ken's id is in, you only need an OR
. 如果您真的只需要查看Ken的ID在哪个列中,则只需要一个
OR
。 The following will produce your example output exactly. 以下将完全产生您的示例输出。
SELECT * FROM Meetings WHERE originatorid = 3 OR Assistantid = 3;
If you need to take the complex route and list names along with meetings, an OR
in your join's ON
clause should work here: 如果您需要采用复杂的路线和名称以及会议名称,则
OR
的ON
子句中的OR
应该在这里起作用:
SELECT
Administrators.name,
Administrators.id,
Meetings.originatorid,
Meetings.Assistantid
FROM Administrators
JOIN Meetings
ON Administrators.id = Meetings.originatorid
OR Administrators.id = Meetings.Assistantid
Where Administrators.name = 'Ken'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.