首页 > 解决方案 > 使用 dolphindb 中的 unionAll 函数连接两个表

问题描述

我创建了两个可以通过 id 列链接的表。table1 中的 x 列是 int。table2 中的 x 列是字符串。我尝试通过调用 unionAll 函数来加入这两个表,但都返回错误:

t1=table(1 2 3 as id, 11 12 13 as x)
t2=table(3 4 5 as id, `a`b`c as x)
unionAll(t1,t2);
/*Error:
unionAll(t1, t2) => Failed to append data to column 'x' 
*/


t1=table(1 2 3 as id, 11 12 13 as x)
t2=table(3 4 5 as id, `a`b`c as x)
unionAll(t1,t2,true);
/*Error:
unionAll(t1, t2, 1) => The data type of column [x] of the input table [#1] is inconsistent                 
with that of other input tables.
*/

标签: dolphindb

解决方案


因为 t1 和 t2 的 x 列中的数据类型不同,所以不能合并到同一列中。可以将t2的x列的列名修改为y,然后使用unionAll连接:

t1=table(1 2 3 as id, 11 12 13 as x)
t2=table(3 4 5 as id, `a `b `c as y)
unionAll(t1,t2, true)
ID X 是的
1 11
2 12
3 13
3 一个
4 b
5 C

推荐阅读