简体   繁体   English

内连接 3 个表不返回任何内容

[英]Inner join 3 tables returns nothing

have 3 tables.有3张桌子。

CB table CB表

      CID   EC ID   Status  CType
      1001  1001    Active  1540
      1004  1001    Active  4

Table M表 M

CID     EID        EC ID    EID-CID
1001      1166          1001    1166_1001
1001      1167a         1001    1167a_1001
1001      1167b         1001    1167b_1001
1001      1168          1001    1168_1001
2014.2071 1043          2018    1043_2014.2071
1004      1166          1001    1166_1004
1004      1167a         1001    1167a_1004
1004      1168          1001    1168_1004
1004      1167b         1001    1167b_1004

Third table(CM)第三表(CM)

EID           CID            EID-CID      EC ID          CType
1043    2014.2071     1043_2014.2071      2018            4

I'm trying to inner join 3 tables.我正在尝试内部连接 ​​3 个表。 And i'm using query below(i refered stackoverflow page Inner Joining three tables ) My query is returns nothing.我正在使用下面的查询(我指的是 stackoverflow 页面内部联接三个表)我的查询不返回任何内容。 but not giving any error messages.但没有给出任何错误信息。

SELECT 
  M.EID, 
  CB.CID, 
  M.[EID-CID], 
  CB.[CType],
  CB.[EC ID] 
FROM 
  (CB INNER JOIN M ON CB.CID = M.CID) 
  inner join CM on CM.CID=M.CID

Result should be like this结果应该是这样的

 CID        EID       EC ID EID-CID      CType
1001        1166    1001    1166_1001       1540
1001        1167a   1001    1167a_1001      1540
1001        1167b   1001    1167b_1001      1540
1001        1168    1001    1168_1001       1540
2014.2071    1043   2018    1043_2014.2071  5.5
1004        1166    1001    1166_1004        4
1004        1167a   1001    1167a_1004       4
1004        1168    1001    1168_1004        4
1004        1167b   1001    1167b_1004       4

When using inner join only results that have a match on the join criteria would be returned.使用inner join只会返回与联接条件匹配的结果。

  1. CB table has two IDs: 1001, 1004 CB表有两个ID: 1001, 1004
  2. M table has both of those IDs (would be returned at this point of the query) M表具有这两个 ID(将在查询的此时返回)
  3. CM table has none of the IDs from CB . CM没有来自CB的 ID。

Doing an inner join between the three tables produces zero results as it should .做一个inner join三个表之间产生任何结果,因为它应该

If you remove the join on CM you'll have results related to the two IDs from CB , or if you were to join only on the M and CM tables you would have your 2014.2071 row, however there are no records that exist throughout all three tables.如果您删除CM上的连接,您将获得与CB的两个 ID 相关的结果,或者如果您MCM表上连接,您将拥有2014.2071行,但是所有三个中都不存在记录表。 Due to using inner joins between all three tables, you are receiving the results you should (based on what you have written), which is zero records.由于在所有三个表之间使用inner joins ,您将收到您应该收到的结果(基于您所写的内容),即零记录。

Inner join only returns results where there is a match.内连接只返回匹配的结果。 It looks like you have join types messed up.看起来你的连接类型搞砸了。 Based on how you say the results should look, I assume you are looking for a LEFT or RIGHT JOIN.根据您所说的结果应该看起来如何,我假设您正在寻找 LEFT 或 RIGHT JOIN。 eg例如

SELECT 
M.EID, CB.CID, M.[EID-CID], CB.[CType],CB.[EC ID] 
FROM 
  (CB RIGHT JOIN M ON CB.CID = M.CID) LEFT JOIN CM  on CM.CID=M.CID

You might want to take a look at this website:http://blog.codinghorror.com/a-visual-explanation-of-sql-joins/你可能想看看这个网站:http ://blog.codinghorror.com/a-visual-explanation-of-sql-joins/

Your join to CM is joining on the wrong columns: you are joking on CID, but you should be joining on EID:您加入 CM 加入了错误的列:您在 CID 上开玩笑,但您应该加入 EID:

SELECT M.EID, CB.CID, M.[EID-CID], CB.[CType],CB.[EC ID]
FROM CB
JOIN M ON CB.CID = M.CID
JOIN CM ON CM.EID = M.EID -- change here

Removed unnecessary brackets.删除了不必要的括号。

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

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