简体   繁体   English

SQLite 简化了左外连接的并集

[英]SQLite simplify union of left outer join

I inherited some monster SQL queries and I would like to simplify/ shorten them as much as possible.我继承了一些怪物 SQL 查询,我想尽可能地简化/缩短它们。

One of them is:其中之一是:

SELECT 
              * 
            FROM 
              (
                SELECT 
                  $columnsFromBothTables
                FROM 
                  table_abc
                  left outer join table_xyz using profile_id 
                union 
                select 
                  $columnsFromBothTables
                from 
                  table_xyz 
                  left outer join table_abc using profile_id
              ) 

It looks weirdly redundant.它看起来奇怪地多余。
Is there a way to simplify it to sth like?有没有办法将其简化为某事?

SELECT * FROM (
    SELECT $columns FROM table_abc MAGIC_OPERATION table_xyz
)

Isn't it a FULL OUTER JOIN?不是全外连接吗?

Updates : My understanding is that both tables have M-1 relationship to "profile" table.更新:我的理解是两个表都与“配置文件”表有 M-1 关系。

They do not have direct relationship to each other他们彼此没有直接关系

This is similar to but not exactly a full outer join .这类似于但不完全是full outer join I would recommend this approach for the full outer join :我会为full outer join推荐这种方法:

select $columns
from abc left join
     xyz
     on . . .
union all
select $columns
from xyz left join
     abc
     on . . .
where abc.? is null;  -- some column to validate that there is no match

Note: This does not remove duplicates -- which your code does.注意:这不会删除重复项 - 您的代码会这样做。 That is why this version should perform better.这就是为什么这个版本应该表现得更好。 But it might not do exactly what you want.但它可能不完全符合您的要求。

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

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