【问题标题】:Postgresql: Select unique rows from two tablesPostgresql:从两个表中选择唯一的行
【发布时间】:2017-02-23 00:54:12
【问题描述】:

我有这两个带有值的表。我需要将所有唯一值组合到 1 个表中。所以结果一定是:

reffnum   leftb  rightb desc date
tes1   1  0    Tes 1    14/10/2016
tes 1  10 0    Tes siji    14/10/2016
tes2   0  12   Tes nomor 2 14/10/2016
tes 3  0  1002 Data baru  15/10/2016
tes1   0  11   Tes 1 baru 15/10/2016
tes1   0  123  Tes 123    15/10/2016

请帮忙,提前谢谢

表 t1:

reffnum leftb   rightb  desc    timestamp
tes1    1   0   Tes 1   2016-10-12 13:47:06.945581
tes1    1   0   Tes siji    2016-10-12 13:47:06.921685
tes 1   10  0   Tes siji    2016-10-03 14:55:32.126814
tes2    0   12  Tes nomor 2 2016-10-03 14:55:32.11081
tes 3   0   1002    Data baru   2016-10-03 14:55:32.094884
tes1    0   11  Tes 1 baru  2016-10-03 14:55:32.078833

还有这个 t2:

reffnum leftb righb desc date
tes1    1   0   Tes 1   2016-10-03 14:49:15.817506
tes1    1   0   Tes siji    2016-10-03 14:33:40.285849
tes 1   10  0   Tes siji    2016-10-03 14:33:40.269887
tes2    0   12  Tes nomor 2 2016-10-03 14:30:57.376459
tes1    0   123 Tes 123 2016-10-03 14:33:40.285849
tes2    0   12  Tes no2 2016-10-03 14:33:40.269887

已编辑:

这是我能做的最接近的:

  1. 我应该在 t2 中找到在 t1 中没有的唯一值:select * from t2 except select * from t1
  2. 然后我在 no 中插入值。 1到t1

但是现在,问题是,查询没有。 1 抛出错误:

[Err] 错误:除了类型 smallint 和没有时区的时间戳无法匹配

【问题讨论】:

  • 所以,我首先找到了一种查找重复项的方法:wiki.postgresql.org/wiki/Deleting_duplicates,然后我使用 excel 进行了一些变通以保留一个原始行。因此,我可以添加独特的数据以与表 t1 结合。

标签: postgresql distinct-values


【解决方案1】:

union 运算符删除重复项,因此您可以使用非常直接的查询:

SELECT * FROM table1
UNION
SELECT * FROM table2

【讨论】:

  • 谢谢,我还是不明白,union 会根据什么列删除重复项?在 excel 中,我可以根据 reffnum、leftb、rightb 删除重复项
  • @bandungeuy 基于选择列表中的所有列
  • 有了新数据(我在t2中添加一个重复数据:tes2-0-12-tesno2),联合仍然给我重复值:tes2-0-12-tes nomor2和tes2-0-12 -tesno2.
  • 如何根据 reffnum、leftb、right 列去除重复项?
猜你喜欢
  • 2016-10-25
  • 2017-10-31
  • 1970-01-01
  • 1970-01-01
  • 2014-05-13
  • 1970-01-01
  • 2014-01-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多