跨数据库连接

时间:2014-05-22 07:32:49

标签: sql join

我需要加入以下三个不同的表: Three tables with relationships 注意:两个不同的数据库(来源:http://i.stack.imgur.com/4kF87.png)。

如何执行以下操作:

  1. 检查某人是否回答了问题
  2. 将表格发件箱中的问题与表格收件箱中的答案进行比较

1 个答案:

答案 0 :(得分:2)

收件箱和发件箱表之间应该存在直接关系,因为问题可以有很多答案。

对于您的场景,请尝试此操作 - 使用Database2.persons和database1.Inbox之间的内部联接来查找回答问题的人数 -

选择p.id作为personid,i.message,i.id作为inboxid from database2.persons p join database1.inbox i on p.phonenumber = i.phonenumber;

接下来,将表格发件箱中的问题与表格收件箱中的答案进行比较 -

选择i.p​​ersonid,o.message作为问题,i.message作为答案 (选择p.id作为personid,i.message from database2.person p join database1.inbox i on p.phonenumber = i.phonenumber)我在i.personid = o.personid上加入database2.outbox o; < / p>

相关问题