简体   繁体   English

SQL比较两个具有公共ID的表,但表2中的ID可能位于两个不同的列中

[英]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: 如果您需要采用复杂的路线和名称以及会议名称,则ORON子句中的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.

相关问题 比较两个SQL表,如果它们在公共输出表1值和表2值中有一个ID,我想始终输出表1的值 - Comparing two SQL tables I want to always output a table 1 value if they have an ID in common output table 1 value and table 2 value SQL比较两个具有公共ID的表 - SQL compare two tables with common id 比较SQL中不同类型的不同表中的两列 - Comparing two columns from different tables of different types in SQL 两个共有2列的SQL表需要在表A中但不在表B中的记录(列1相同,但列2不同) - two SQL tables with 2 columns in common, need records that are in table A but not in table B (column 1 is same in both but different column 2) 将一个基于结果的组中两个不同表的两列按另一表的 ID 求和 - SUM two columns of two different tables in one result based group by id of another table 比较两个不同表中的两列? - Comparing two columns in two different tables? 比较两个不同表中的两列 - Comparing two columns in two different tables 如何获得两个不同表的两列,其中id = id和id = 9? - How to get two columns of two different tables where id = id and id= 9? 插入两个表并影响其中一个表列的ID到另一个表中 - Insert into two tables and affect the id of one of the table columns into the other table 在sqlite中使用行id比较两个表 - comparing two tables using row id in sqlite
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM