简体   繁体   English

SQL Left Join返回NULL值

[英]SQL Left Join returning NULL value

I have a databse with tables schedule and a table teams (as can be seen in image below) Im trying to use the homeID & awayID inside the the schedule table to do a join on the teams table....Problem is it is returning NULL 我有一个包含表schedule和表teams的数据库(如下图所示),我试图使用schedule表中的homeIDawayIDteams表上进行awayID 。...问题是它正在返回空值

DB Layout 数据库布局

在此处输入图片说明

Schedule Table Layout Schedule表布局

在此处输入图片说明

teams table layout teams表布局

在此处输入图片说明

My Query 我的查询

SELECT s.*,
  t1.teamId as homeId_teamId,
  t1.teamCode as homeId_teamCode,
  t1.teamName as homeId_teamName,
  t2.teamId as visitorId_teamId,
  t2.teamCode as visitorId_teamCode,
  t2.teamName as visitorId_teamName
FROM schedule s
  LEFT JOIN teams t1 ON s.homeId = t1.teamName
  LEFT JOIN teams t2 ON s.visitorId = t2.teamName
WHERE  gameID = '1';

Returned Result 返回结果

在此处输入图片说明

Any idea why am I getting a NULL value for the join? 知道为什么我要为联接获取NULL值吗? What am I missing here? 我在这里想念什么? Any help appreciated. 任何帮助表示赞赏。

id s should be joined to id s (not names): id s应该加入id s(不是名称):

SELECT s.*,
      t1.teamId as homeId_teamId,
      t1.teamCode as homeId_teamCode,
      t1.teamName as homeId_teamName,
      t2.teamId as visitorId_teamId,
      t2.teamCode as visitorId_teamCode,
      t2.teamName as visitorId_teamName
FROM schedule s LEFT JOIN
     teams t1
     ON s.homeId = t1.teamId LEFT JOIN
     teams t2
     ON s.visitorId = t2.teamId
WHERE s.gameID = 1;

gameID is an integer, so the comparison should be to a number, not a string. gameID是一个整数,因此比较应该是数字,而不是字符串。

I note that the tpes of homeId and visitorId don't match teams.teamId , but naming suggests that this is the right condition. 我注意到的热塑性弹性体homeIdvisitorId不匹配teams.teamId ,但命名表明,这是一个正确的状态。 If foreign keys were properly defined, then the types would have to match. 如果正确定义了外键,则类型必须匹配。

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

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