我有2个表,尝试inner join
到一个表中。我使用Excel进行查询,因此语法有限(Jet Access)。 stack1
和stack2
表格相同,位于两个不同的Excel表格中。
这些表来自Vertica(stack1
和stack2
)和SQL Server(overflow
),然后我尝试使用内部查询来组合表。
以下是我的尝试,我收到了一个语法错误,不知道我哪里错了:
select *
from [stack1$], [stack2$]
INNER JOIN [overflow$]
ON [stack1$].[iddate] = [overflow$].[iddate]
AND [stack1$].[idbusiness] = [overflow$].[idbusiness]
AND [stack2$].[iddate] = [overflow$].[iddate]
AND [stack2$].[idbusiness] = [overflow$].[idbusiness]
如果我只做一个这样的表,它可以正常工作:
select *
from [stack1$]
INNER JOIN [overflow$]
ON [stack1$].[iddate] = [overflow$].[iddate]
AND [stack1$].[idbusiness] = [overflow$].[idbusiness]
示例数据:
溢出:
+----------+------------+---------+
| iddate | idbusiness | otherid |
+----------+------------+---------+
| 20180209 | 95971462 | 5235 |
+----------+------------+---------+
堆栈1:
+------------+-------+----------+
| idbusiness | value | iddate |
+------------+-------+----------+
| 95971462 | | 20180209 |
| 95971462 | | 20180209 |
| 95971462 | | 20180209 |
| 95971462 | | 20180209 |
| 95971462 | | 20180209 |
| 95971462 | | 20180209 |
| 95971462 | | 20180209 |
| 95971462 | 10.04 | 20180209 |
+------------+-------+----------+
上面的查询给了我:
+--------------------+-------+----------------+---------+
| stack1$.idbusiness | value | stack1$.iddate | otherid |
+--------------------+-------+----------------+---------+
| 95971462 | | 20180209 | 5235 |
| 95971462 | | 20180209 | 5235 |
| 95971462 | | 20180209 | 5235 |
| 95971462 | | 20180209 | 5235 |
| 95971462 | | 20180209 | 5235 |
| 95971462 | 10.04 | 20180209 | 5235 |
| 95971462 | | 20180209 | 5235 |
| 95971462 | | 20180209 | 5235 |
+--------------------+-------+----------------+---------+
现在我想向此添加Stack 2
,其中包含与stack1
相同的列,但在idbusiness
中包含stack1
。同样看起来不需要iddate
,所有记录都是相同的日期。
答案 0 :(得分:2)
尝试使用Union,它将是(overflow $ join stack1 $)union的唯一行(overflow $ join stack2)。
SELECT * FROM [overflow$]
JOIN [stack1$]
ON [stack1$].[iddate] = [overflow$].[iddate]
AND [stack1$].[idbusiness] = [overflow$].[idbusiness]
UNION
SELECT * FROM [overflow$]
JOIN [stack2$]
ON [stack2$].[iddate] = [overflow$].[iddate]
AND [stack2$].[idbusiness] = [overflow$].[idbusiness]
答案 1 :(得分:1)
不要混合使用旧式和新式连接。
这样的事情应该有效:
select *
from ([overflow$] as o inner join
[stack1$] as s1
on s1.[iddate] = o.[iddate] and
s1.[idbusiness] = o.[idbusiness]
) inner join
[stack2$] as s2
on s2.[iddate] = o.[iddate] and
s2.[idbusiness] = o.[idbusiness];
我使用MS Access语法编写了这个。其他数据库中不需要括号。
答案 2 :(得分:1)
就像戈登所说,不建议在同一个查询中混合隐式和显式联接。以下是使用隐式连接编写查询的方式:
SELECT *
FROM [overflow$], [stack1$], [stack2$]
WHERE [stack1$].[iddate] = [overflow$].[iddate]
AND [stack1$].[idbusiness] = [overflow$].[idbusiness]
AND [stack2$].[iddate] = [overflow$].[iddate]
AND [stack2$].[idbusiness] = [overflow$].[idbusiness]
为了完整起见,以下是使用显式连接编写查询的方式(如Gordon的答案,但使用您的命名方式):
SELECT *
FROM ([overflow$]
INNER JOIN [stack1$]
ON [stack1$].[iddate] = [overflow$].[iddate]
AND [stack1$].[idbusiness] = [overflow$].[idbusiness])
INNER JOIN [stack2$]
ON [stack2$].[iddate] = [overflow$].[iddate]
AND [stack2$].[idbusiness] = [overflow$].[idbusiness]
通常建议使用显式连接。遗憾的是,Jet语法需要用于连接的括号,这使得编写和输入更加困难。读取。
如果这些条件正确,则上述两个查询应该相同并给出相同的结果。但是,某些数据库存在隐式连接的问题/错误,因此结果(主要是性能)可能会有所不同。我不了解Vertica,但对于最新版本的SQL Server,应该没有区别。
如果您没有从上述查询中获取记录,那么条件可能是错误的。我怀疑您确实希望将两个查询的结果与UNION
结合使用:
SELECT *
FROM [overflow$]
INNER JOIN [stack1$]
ON [stack1$].[iddate] = [overflow$].[iddate]
AND [stack1$].[idbusiness] = [overflow$].[idbusiness]
UNION ALL
SELECT *
FROM [overflow$]
INNER JOIN [stack2$]
ON [stack2$].[iddate] = [overflow$].[iddate]
AND [stack2$].[idbusiness] = [overflow$].[idbusiness]