RIGHT JOIN将3个表连接到1个左表,并将其他2个表中的列连接到右侧

时间:2012-12-27 23:25:59

标签: mysql

我正在创建一个社交网络,我有一个非常具体的案例,我几乎得到了正确的查询。我有6个表,我正在合并到一个查询中。 表recent用于查找表brostplainegobrostbrostpublic上的确切行,当在members和{egos上找到匹配项时,它会变得复杂{1}}表,其中members.username将与列ID上的brostplainbrostpublic匹配,而egos.egoname将与列egobrost上的egoid匹配。所有这些都在最近的.realtime列中按unix时间戳排序。这是当前的查询:

   SELECT recent.id, 
          recent.brosttype, 
          recent.postid, 
          recent.realtime, 
          members.username,
          egos.egoname, 
          brostplain.id, 
          brostplain.postid, 
          brostplain.content, 
          brostplain.up, 
          brostplain.down, 
          brostplain.timedata, 
          brostplain.realtime, 
          brostplain.attached, 
          brostplain.replies, 
          brostplain.ext, 
          brostplain.onpage, 
          brostplain.touser, 
          brostplain.isego, 
          brostplain.category, 
          egobrost.egoid, 
          egobrost.postid, 
          egobrost.content, 
          egobrost.up, 
          egobrost.down, 
          egobrost.timedata, 
          egobrost.realtime, 
          egobrost.attached, 
          egobrost.replies, 
          egobrost.ext, 
          egobrost.onpage, 
          egobrost.touser, 
          egobrost.isego, 
          egobrost.category, 
          brostpublic.id, 
          brostpublic.postid, 
          brostpublic.content, 
          brostpublic.up, 
          brostpublic.down, 
          brostpublic.timedata, 
          brostpublic.realtime, 
          brostpublic.attached, 
          brostpublic.replies, 
          brostpublic.ext, 
          brostpublic.category, 
          brostpublic.isego 
     FROM recent 
LEFT JOIN brostplain ON recent.brosttype=brostplain.brosttype 
      AND recent.postid=brostplain.postid 
LEFT JOIN brostpublic ON recent.brosttype=brostpublic.brosttype 
      AND recent.postid=brostpublic.postid 
LEFT JOIN egobrost ON recent.brosttype=egobrost.brosttype 
      AND recent.postid=egobrost.postid 
LEFT JOIN egos ON egos.egoid=egobrost.egoid
     JOIN members ON brostplain.id=members.id OR brostpublic.id=members.id 
    WHERE recent.id='2' ORDER BY recent.realtime DESC

现在这会得到所有相关的行,但由于egos表的连接方式,它不会返回egobrost行的任何结果,我无法弄清楚连接的放置位置或如何实现它。如果我省略了自我的JOIN并从我的SELECT中删除了egos.egoname,它将显示egobrost表上的行没问题,但我需要egoname作为我的输出。任何帮助将不胜感激。

编辑(解决):经过深思熟虑后,我通过在members上的JOIN添加LEFT解决了这个问题。现在结果集已完成。感谢那些贡献的人。

1 个答案:

答案 0 :(得分:0)

通过将成员上的JOIN更改为LEFT JOIN,与自我上的LEFT JOIN重合来解决。这是最终查询:

SELECT recent.id, 
          recent.brosttype, 
          recent.postid, 
          recent.realtime, 
          members.username,
          egos.egoname, 
          brostplain.id, 
          brostplain.postid, 
          brostplain.content, 
          brostplain.up, 
          brostplain.down, 
          brostplain.timedata, 
          brostplain.realtime, 
          brostplain.attached, 
          brostplain.replies, 
          brostplain.ext, 
          brostplain.onpage, 
          brostplain.touser, 
          brostplain.isego, 
          brostplain.category, 
          egobrost.egoid, 
          egobrost.postid, 
          egobrost.content, 
          egobrost.up, 
          egobrost.down, 
          egobrost.timedata, 
          egobrost.realtime, 
          egobrost.attached, 
          egobrost.replies, 
          egobrost.ext, 
          egobrost.onpage, 
          egobrost.touser, 
          egobrost.isego, 
          egobrost.category, 
          brostpublic.id, 
          brostpublic.postid, 
          brostpublic.content, 
          brostpublic.up, 
          brostpublic.down, 
          brostpublic.timedata, 
          brostpublic.realtime, 
          brostpublic.attached, 
          brostpublic.replies, 
          brostpublic.ext, 
          brostpublic.category, 
          brostpublic.isego 
     FROM recent 
LEFT JOIN brostplain ON recent.brosttype=brostplain.brosttype 
      AND recent.postid=brostplain.postid 
LEFT JOIN brostpublic ON recent.brosttype=brostpublic.brosttype 
      AND recent.postid=brostpublic.postid 
LEFT JOIN egobrost ON recent.brosttype=egobrost.brosttype 
      AND recent.postid=egobrost.postid 
LEFT JOIN egos ON egos.egoid=egobrost.egoid
LEFT JOIN members ON brostplain.id=members.id OR brostpublic.id=members.id 
    WHERE recent.id='2' ORDER BY recent.realtime DESC