简体   繁体   English

SQL - 使用 UNION 加入? UNION 使用 JOIN?

[英]SQL - JOIN using UNION ?? UNION using JOIN?

I was asked this question during one of my interviews.我在一次采访中被问到这个问题。 Can you do JOIN using UNION keyword?您可以使用 UNION 关键字进行 JOIN 吗? Can you do UNION using JOIN keyword?你能用 JOIN 关键字做 UNION 吗?

That is - 1. I should get same output as JOIN without using JOIN keyword, but using UNION Keyword?那就是 - 1.我应该得到与 JOIN 相同的 output 而不使用 JOIN 关键字,但使用 UNION 关键字? 2. I should get same output as UNION without using UNION keyword, but using JOIN Keyword? 2. 我应该得到与 UNION 相同的 output 而不使用 UNION 关键字,但使用 JOIN 关键字?

Can you give me an example of how to do this if possible?如果可能的话,你能给我一个例子来说明如何做到这一点吗?

As this is an interview question, they are testing your understanding of both these functions.由于这是一个面试问题,他们正在测试您对这两个功能的理解。

The likely answer they are expecting is "generally no you cannot do this as they perform different actions", and you would explain this in more detail by stating that a union appends rows to the end of the result set where as a join adds further columns.他们期望的可能答案是“通常不,您不能这样做,因为他们执行不同的操作”,并且您可以通过说明联合将行附加到结果集的末尾来更详细地解释这一点,其中作为连接添加更多列.

The only way you could have a Join and a Union work is where rows contain data from only one of the two sources:您可以让 Join 和 Union 工作的唯一方法是行仅包含来自两个源之一的数据:

SELECT A.AA, '' AS BB FROM A
UNION ALL 
SELECT '' AS AA, B.BB FROM B

Is the same as:是相同的:

SELECT ISNULL(A.AA, '') AS AA, ISNULL(B.BB, '') AS BB FROM A
FULL OUTER JOIN B ON 1=0

Or to do this with only one column where the types match:或者仅使用类型匹配的一列执行此操作:

SELECT A.AA AS TT FROM A
UNION ALL 
SELECT B.BB AS TT FROM B

Is the same as:是相同的:

SELECT ISNULL(A.AA, B.AA) AS TT
FROM A
FULL OUTER JOIN B ON 1=0

One case where you would do this is if you have data spawned over multiple tables but you want to see ti all together, however I would advise to use a UNION in this case rather than a FULL OUTER JOIN because of the query is doing what you would otherwise expect.您会这样做的一种情况是,如果您在多个表上生成了数据,但您希望一起查看 ti,但是我建议在这种情况下使用 UNION 而不是 FULL OUTER JOIN,因为查询正在执行您的操作否则会期望。

An interview is the framework on which you set out your wares.面试是您展示商品的框架。 Remember: don't answer questions;)记住:不要回答问题;)

Think of a press conference: the spokesperson is not looking to answer difficult questions from journos to catch themselves out.想想新闻发布会:发言人不想回答记者的棘手问题来让自己脱颖而出。 Rather, they are looking for questions to which they already have answers, being the information they want to release (and no more!)相反,他们正在寻找他们已经有答案的问题,即他们想要发布的信息(仅此而已!)

If I faced this question in an interview, I would use it to demonstrate my knowledge of relational algebra because that's what I'd have gone into the interview with the intention of doing;如果我在面试中遇到这个问题,我会用它来证明我对关系代数的了解,因为这就是我进入面试的意图; I be alert for the "Talk about relational algebra here" question and this would be it.我对“在这里谈论关系代数”问题保持警惕,就是这样。

Loosely speaking, JOIN is the counterpart of logical AND, whereas UNION is the counterpart of logical OR.粗略地说,JOIN 是逻辑 AND 的对应物,而 UNION 是逻辑 OR 的对应物。 Therefore, similar questions using convention logic could be, "Can you do AND using OR?"因此,使用约定逻辑的类似问题可能是,“你能用 OR 做 AND 吗?” and "Can you do OR using AND?"和“你能用 AND 做 OR 吗?” The answer would depend on what else you could use eg NOT might come in handy;)答案将取决于您还可以使用什么,例如 NOT 可能会派上用场;)

I'd also be tempted to discuss the differences between the set of primitive operators, the set of operators necessary for computational completeness and the set of operators and shorthands required for practical purposes.我也很想讨论原始运算符集、计算完整性所需的运算符集以及实际目的所需的运算符集和速记集之间的区别。

Trying to answer the question directly raises further questions.试图直接回答这个问题会引发进一步的问题。 JOIN implies 'natural join' in relational algebra whereas in SQL it implies INNER JOIN . JOIN 在关系代数中意味着“自然连接”,而在 SQL 中则意味着INNER JOIN If the question specifically relates to SQL, do you have to answer for all the JOIN types?如果问题具体涉及 SQL,您是否必须回答所有JOIN类型? What about UNION JOIN ? UNION JOIN呢?

To employ one example, SQL's outer join is famously a UNION .举一个例子,SQL 的外连接是著名的UNION Chris Date expresses it better than I could ever hope to: Chris Date 表达得比我想象的要好:

Outer join is expressly designed to produce nulls in its result and should therefore be avoided, in general.外连接被明确设计为在其结果中产生空值,因此通常应该避免。 Relationally speaking, it's a kind of shotgun marriage: It forces tables into a kind of union—yes, I do mean union, not join—even when the tables in question fail to conform to the usual requirements for union (see Chapter 6).从关系上讲,这是一种霰弹枪婚姻:它迫使表成为一种联合——是的,我的意思是联合,而不是联合——即使所讨论的表不符合联合的通常要求(参见第 6 章)。 It does this, in effect, by padding one or both of the tables with nulls before doing the union, thereby making them conform to those usual requirements after all.实际上,它是通过在执行联合之前用空值填充一个或两个表来做到这一点的,从而使它们最终符合那些通常的要求。 But there's no reason why that padding shouldn't be done with proper values instead of nulls但是没有理由不应该使用正确的值而不是空值来完成填充

SQL and Relational Theory, 1st Edition by C.J. SQL 和关系理论,第一版 C.J. Date 日期

This would be a good discussion point if, "I hate nulls" is something you wanted to get across in the interview!如果“我讨厌空值”是您想在面试中传达的内容,这将是一个很好的讨论点!

These are just a few thoughts that spring to mind.这些只是 spring 想到的一些想法。 The crucial point is, by asking these questions the interviewer is offering you a branch.关键是,通过问这些问题,面试官为你提供了一个分支。 What will YOU hang on it?你会在上面挂什么? ;) ;)

Do you mean something like this?你的意思是这样的吗?

create table Test1 (TextField nvarchar(50), NumField int)
create table Test2 (NumField int)
create table Test3 (TextField nvarchar(50), NumField int)

insert into Test1 values ('test1a', 1)
insert into Test1 values ('test1b', 2)
insert into Test2 values (1)
insert into Test3 values ('test3a', 4)
insert into Test3 values ('test3b', 5)

select Test1.*
from Test1 inner join Test2 on Test1.NumField = Test2.NumField
union
select * from Test3

(written on SQL Server 2008) (写在 SQL 服务器 2008 上)

UNION works when both SELECT statements have the same number of columns, AND the columns have the same (or at least similar) data types.当两个SELECT语句具有相同数量的列并且这些列具有相同(或至少相似)数据类型时, UNION起作用。
UNION doesn't care if both SELECT statements select data only from a single table, or if one or both of them are already JOIN s on more than one table. UNION不关心SELECT语句 select 数据是否仅来自单个表,或者它们中的一个或两个是否已经在多个表上JOIN

I think it also depends on other operations available.我认为这也取决于其他可用的操作。

If I remember well, UNION can be done using a FULL OUTER join:如果我没记错的话,可以使用FULL OUTER join 来完成UNION

Table a (x, y)

Table b (x, y) 

CREATE VIEW one
AS
SELECT a.x AS Lx
     , b.x AS Rx
     , a.y AS Ly
     , b.y AS Ry
FROM a FULL OUTER JOIN b
         ON  a.x = b.x
         AND a.y = b.y


CREATE VIEW unionTheHardWay
AS
SELECT COALESCE(Lx, Rx) AS x
     , COALESCE(Ly, Ry) AS y
FROM one

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

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