简体   繁体   English

具有不同主键的SQL innerjoin 3表

[英]SQL innerjoin 3 tables with different PRIMARY keys

I am trying to join 3 tables together using the following sql command: 我正在尝试使用以下sql命令将3个表连接在一起:

SELECT 
    tg.Kickoff,
    tg.League,
    tg.home,
    tg.away,
    ln.Full,
    ln.Mysite,
    ln.Country,
    tl.mydb
  FROM `todaysgames` AS tg
 INNER JOIN `leaguenames` AS ln ON tg.League = ln.Othersites
 INNER JOIN `teamlookup` AS tl ON tg.home = tl.websitedb
                                OR tg.away = tl.websitedb;

The issue is I get is it doubles each row. 问题是我得到的是每行加倍。

What I am looking to do is for each tg.home and tg.away is replace it with the tl.websitedb (this basically amends the name). 我要为每个tg.home和tg.away做的事情是将其替换为tl.websitedb(这基本上是对名称的修改)。

would this be possible? 这可能吗? (Below is the current output) (下面是当前输出)

在此处输入图片说明

here is desired output 这是所需的输出 在此处输入图片说明

If I interprete your question correctly, you get two records per game where you only want one? 如果我正确地解释了您的问题,那么您每场比赛只得到两个记录,而您只想要一个? This is because you join with teamlookup on tow conditions, thus finding two records per game. 这是因为您在拖曳条件下与teamlookup一起加入,因此每场比赛找到两个记录。

Example: 例:

todaysgames todaysgames

Game  Home  Away
1     A     B

teamlookup teamlookup

websitedb  mydb
A          DBA
B          DBB

result 结果

Game  Home  Away  mydb
1     A     B     DBA
1     A     B     DBB

I guess you really want to get this instead: 我想你真的想得到这个:

Game  Home  Away  mydbHome  mydbAway
1     A     B     DBA       DBB

Query: 查询:

SELECT 
  tg.Kickoff,
  tg.League,
  tg.home,
  tg.away,
  ln.Full,
  ln.Mysite,
  ln.Country,
  tlh.mydb as mydb_home,
  tla.mydb as mydb_away
FROM todaysgames tg
JOIN leaguenames ln ON tg.League = ln.Othersites
JOIN teamlookup tlh ON tg.home = tlh.websitedb
JOIN teamlookup tla ON tg.away = tla.websitedb;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM