简体   繁体   English

具有一对一和一对多关系的SQL Join表

[英]SQL Join tables that have both a one to one and one to many relationship

The title is probably a bad title but I cannot think of a better one... 标题可能是一个不好的标题,但我想不到更好的标题...

I have a kind of unique schema, where that I have 5 tables with relationships, but one of the tables has a relationship with two tables. 我有一种独特的模式,其中有5个具有关系的表,但是其中一个表与两个表有关系。 To make it less confusing, let me show you the schema: 为了减少混乱,让我向您展示模式:

Table A: 表A:

AID BID Name
1   1   101
2   1   102
3   1   103
4   2   104
5   3   105
6   4   106

Where AID is the Primary ID to table A and BID is a primary ID to table B 其中AID是表A的主ID,BID是表B的主ID

Table B: 表B:

BID   CID   DID   Name
1     null  3     101
2     null  4     102
3     1     null  103
4     2     null  104

where either CID is null or DID is null 其中CID为null或DID为null

Table C: 表C:

CID   DID   Name
1     1     A
2     2     B
3     3     A
4     4     B
5     5     C
6     5     A

Table D: 表D:

DID   EID   Name
1     1     Alpha
2     1     Bravo
3     1     Charlie
4     1     Echo
5     2     Delta

Table E: 表E:

EID   Name
1     Home
2     Away

I know this may be a bit confusing but basically the data can do one of two things: 我知道这可能有点令人困惑,但是基本上数据可以完成以下两项操作之一:

  1. The data in table A can connect to B, then C, then D then E, or 表A中的数据可以连接到B,然后是C,然后是D,然后是E,或者
  2. The data in table A can connect to B, then D, then E 表A中的数据可以连接到B,然后连接D,然后连接E

My issue is that I want to create a SQL query that will Join all of the tables, grabbing A.Name, A.AID, and E.Name from any row where B.Name LIKE '%Some name%', C.Name LIKE '%Some name%', D.Name LIKE '%Some name%', and E.Name LIKE '%Some name%'. 我的问题是我想创建一个将联接所有表的SQL查询,从其中B.Name喜欢'%Some name%',C.Name的任何行中获取A.Name,A.AID和E.Name。 LIKE'%Some name%',D.Name LIKE'%Some name%'和E.Name LIKE'%Some name%'。

so for example, I want a query that if I set E.Name LIKE '%Home%', the query will return: 因此,例如,我想要一个查询,如果我设置了E.Name LIKE'%Home%',查询将返回:

E.Name    E.EID    D.DID    C.CID    B.BID    A.AID    A.Name
Home      1        3        null     1        1        101
Home      1        3        null     1        2        102
Home      1        3        null     1        3        103
Home      1        4        null     2        4        103
Home      1        1        1        3        5        103
Home      1        2        2        4        6        103

Currently my current query only returns "random" data, and by that I mean it returns something but not the right thing and I can't figure out what the rows it is returning have in common that the query is pulling. 当前,我当前的查询仅返回“随机”数据,这意味着它返回的内容不是正确的东西,而且我无法弄清楚它返回的行与查询拉的共同点。 But basically my query is: 但基本上我的查询是:

select ... from tableA, 
inner join tableB on A.BID=B.BID 
inner join tableC on B.CID=C.CID 
inner join tableD on C.DID=D.DID OR B.DID=D.DID 
inner join E.EID = D.EID
WHERE E.Name LIKE '%Home%';

Any suggestions would be great! 任何建议都很好! Thank you!!! 谢谢!!!

SELECT  *
FROM    a
JOIN    b
USING   (bid)
LEFT JOIN
        с
USING   (cid)
JOIN    d
ON      d.did = COALESCE(c.did, b.did)
JOIN    e
USING   (eid)
WHERE   e.name LIKE '%home%'

If you change a couple of your INNER JOINs to LEFT JOINs you should get your desired results. 如果将几个INNER JOIN更改为LEFT JOIN,您应该会得到所需的结果。

SELECT e.Name, e.EID, d.DID, c.CID, b.BID, a.AID, a.Name
FROM TableA a
INNER JOIN TableB b ON a.BID = b.BID
LEFT JOIN TableC c ON b.CID = c.CID
INNER JOIN TableD d ON c.DID = d.DID or b.DID = d.DID
INNER JOIN TableE e ON d.EID = e.EID
WHERE e.Name LIKE '%Home%'

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

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