简体   繁体   English

mysql结合多个联接

[英]mysql combine multiple joins

I've been struggling with altering my query for days now and I just can't get the result I want. 几天来,我一直在努力更改查询,但无法获得想要的结果。 I kind of hoping someone can help me out how to change my query. 我希望有人能帮助我如何更改查询。

Having the following query now: 现在具有以下查询:

SELECT 
    t.tId,
    t.tName,
    a.aId,
    a.aName,
    u.uId,
    u.uName
FROM
    tableT t
    LEFT JOIN t_a ta USING (tId)
    LEFT JOIN t_u tu USING (tId)
    LEFT JOIN tableA a ON a.aId = ta.aId
    LEFT JOIN tableU u ON u.uId = tu.uId

It returns rows from tableT with matched row in tableA and tableU. 它从tableT返回的行与tableA和tableU中的匹配行。 Now I have to change it to get a result where all row from tableA are returned as well. 现在,我必须对其进行更改以获取返回tableA的所有行的结果。

If I'm not mistaken I want a FULL OUTER JOIN with tableA and a LEFT JOIN with tableU. 如果我没记错的话,我想使用tableA进行FULL OUTER JOIN,使用tableU进行LEFT JOIN。 (I'm aware that FULL OUTER JOIN doesn't exists, which makes it even more confusing for me) (我知道FULL OUTER JOIN不存在,这对我来说更加令人困惑)

table t_a contains many to many relations and table t_u contains many to one relations from tableT's respective. 表t_a包含多对多关系,表t_u包含来自tableT的多对一关系。

In short, when I search for a value, I want to have the following rows to be returned: Any tableT row containing the value and matched rows from tableU and matched rows from tableA. 简而言之,当我搜索值时,我希望返回以下行:包含值的任何tableT行以及来自tableU的匹配行来自tableA的匹配行。 Any other (unmatched) tableA row containing the value. 任何其他(不匹配)表包含该值的行。 No other (unmatched) rows from tableU. 没有来自tableU的其他(不匹配)行。

Values needed before: 之前需要的值:

tableT   tableA    tableU
ANY      ANY       ANY
ANY      NULL      ANY
ANY      NULL      NULL

Values needed now: 现在需要的值:

  tableT      tableA        tableU
  ANY          ANY           ANY
  ANY          NULL          ANY
  ANY          NULL          NULL
**NULL**     **ANY**       **NULL**

My apologies when I'm asking for help under the wrong circumstances. 在错误的情况下寻求帮助时,我深表歉意。 Thanks in advance for any help. 在此先感谢您的帮助。

A have a fiddle here: http://sqlfiddle.com/#!2/f6301/11 一个小提琴在这里: http ://sqlfiddle.com/#!2/ f6301/11

It returns all desired rows except for the row containing: 它返回除包含以下内容的行之外的所有所需行:

TID    TNAME    AID    ANAME    UID    UNAME
NULL   NULL     4      aName4   NULL   NULL

I think what you want is to start with all the tid s from tableT and t_a : 我认为您要从tableTt_a所有tid开始:

FROM ((SELECT t.tID from tableT) UNION
      (SELECT ta.tID from tableA t_a)
     ) ids left outer join
     tableT t
     USING (tId) LEFT JOIN
     t_i ti
     USING (tId) LEFT JOIN
     tableA a
     ON a.aId = ta.aId LEFT JOIN
     tableU u
     ON u.uId = ti.uId

table t_a contains many to many relations and table t_u contains many to one relations from tableT's respective. 表t_a包含多对多关系,表t_u包含来自tableT的多对一关系。

But there is no relationship between t_a and t_u. 但是t_a和t_u之间没有关系。 Yet you are trying to get them to share tuple space with each other. 但是,您正在尝试使它们彼此共享元组空间。

Look at it like this. 这样看。 TableT is a super class. TableT是超类。 Table t_a extends TableT to form a class of its own. 表t_a扩展了TableT以形成其自己的类。 Table t_u also extends TableT to form a class of its own. 表t_u还扩展了TableT以形成其自己的类。 So, while t_a and t_u both extend TableT, they form separate classes (entities) that are really independent of each other. 因此,尽管t_a和t_u都扩展了TableT,但它们形成了真正彼此独立的单独的类(实体)。 There is really no way to shoehorn them into one entity expression (tuple). 确实没有办法将它们简化为一个实体表达式(元组)。

To put in yet another way, you are trying to mix apples and oranges and a relational database does not do fruit salad very well. 换句话说,您正在尝试将苹果和橙子混合使用,并且关系数据库不能很好地完成水果沙拉。

If I'm not mistaken the query I need is as follows: 如果我没有记错的话,我需要的查询如下:

SELECT
    t.tId,
    t.tName,
    a.aId,
    a.aName,
    u.uId,
    u.uName
FROM
    tableT t
    LEFT JOIN t_a ta USING (tId)
    LEFT JOIN t_u ti USING (tId)
    LEFT JOIN tableA a USING (aId)
    LEFT JOIN tableU u USING (uId)
UNION
SELECT
    t.tId,
    t.tName,
    a.aId,
    a.aName,
    NULL,
    NULL
FROM
    tableT t
    RIGHT JOIN t_a ta USING (tId)
    RIGHT JOIN tableA a USING (aId)
    WHERE t.tId IS NULL

But I'm sure it can be optimized or different. 但我敢肯定,它可以被优化或有所不同。

It will/should now return all rows from tableT with possible related tableA data and possible related tableU data and in addition any other tableA rows. 现在/它将返回来自tableT的所有行,以及可能的相关tableA数据和可能的相关tableU数据,以及其他任何tableA行。

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

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