我想使用完整的外连接来连接两个包含记录(id,value)的表。这两个表在ID字段上没有交集,但我想通过ID字段加入它们。我正在尝试使用完整的外连接来完成此连接,但我不明白。需要帮助了解完整的外连接
我正在使用DB2。
到目前为止,我有(用手单独执行每条语句):
create table S1 (
id integer primary key not null,
value integer not null)
create table S2 (
id integer primary key not null,
value integer not null)
insert into S1 values (0, 50)
insert into S1 values (1, 20)
insert into S2 values (2, 40)
insert into S2 values (3, 90)
select * from S1 full outer join S2
我得到了全外的错误连接尝试:
SQL0104N An unexpected token "join S2" was found following "* from S1 full
outer". Expected tokens may include: "<space>". SQLSTATE=42601
编辑:谢谢你!我得到它的工作,但它不是我所期望的。
select * from S1 full outer join S2 on S1.id = S2.id
的问题是,现在它显示(:表示空格)
+--+-----+--+-----+
|ID|VALUE|ID|VALUE|
+--+-----+--+-----+
|-:|-::::|3:|90:::|
+--+-----+--+-----+
|-:|-::::|2:|40:::|
+--+-----+--+-----+
|1:|20:::|-:|-::::|
+--+-----+--+-----+
|0:|50:::|-:|-::::|
+--+-----+--+-----+
我不想重复的领域。有没有办法让表加入:
+--+-----+
|ID|VALUE|
+--+-----+
表?
我尝试使用
select id from s1
full outer join s2 on s1.id = s2.id
不用说,我得到
SQL0203N A reference to column "ID" is ambiguous. SQLSTATE=42702
解决了!
select coalesce(s1.id, s2.id) as id, \
coalesce (s1.value, s2.value) as value \
from s1 full outer join s2 on s1.id = s2.id and s1.value = s2.value
group by id asc
和d士丹利另一种解决方案(mofidied)
SELECT Id, Value \
FROM S1 \
UNION ALL \
SELECT Id, Value \
FROM S2 \
ORDER BY ID ASC
谢谢!
你仍然需要去说你参加什么。例如。 'ON s1.id = s2.id'。这只是一个不需要ON的交叉连接。 – GarethD
仍然需要on子句...将“on 1 = 1”添加到查询的结尾处,为此功能创建一个交叉连接 – Twelfth
谢谢!这有效,但现在我有一个格式问题...任何帮助表示赞赏! – Dmitry