sql - SQL 合并 - 在结果输出中填充合并值的其他方法?
问题描述
我想加入两个表
have1
key ...
a ...
b ...
c ...
have2
key ...
a ...
c ...
d ...
并获得类似的输出
want
key ...
a ...
b ...
c ...
d ...
我知道
create table want as
select coalesce(a.key, b.key) as key, ...,
from have1 a full join have2 b
on a.key=b.key;
会给我输出,但有其他选择吗?我想要更简洁易读的代码,并且加入 3 或 4 个条件似乎需要大量文本才能实现所需的输出(例如,相对于 SAS 数据步骤)。
解决方案
您可以使用union all
:
select h1.*
from have1 h1
union all
select h2.*
from h2
where not exists (select 1 from have1 h1 where h1.key = h2.key);
推荐阅读
- react-native - React native:崩溃时不显示有用的错误堆栈
- wpf - WPF .NET 项目没有 .csproj 文件
- php - 任何人都知道我可以如何在 PHPMyAdmin 中隐藏表格?
- python - Django:值违反非空约束,但是值不是正在创建的模型的一部分
- c# - 是否可以将选定项目从 Datagrideview 复制到剪贴板 c#
- python - Foursquare API 的探索端点在我请求时返回不同的结果
- python-3.x - 我们可以使用一些构造函数初始化具有不同形状的 numpy 数组吗?
- javascript - 对单个 Firestore 文档进行实时更新
- vue.js - Vue.js mapbox-gl 错误:无效类型:“容器”必须是字符串或 HTMLElement
- svelte - 有没有办法在 Svelte 中不使用组件道具来获取文本