Postgresql在多个连接上返回重复数据

时间:2018-04-16 17:50:37

标签: sql postgresql

我无法弄清楚为什么我的输出是重复的,但我确定它是不正确的连接。我一般都是SQL的新手,所以这可能是一个无用的查询。

SELECT a.id, a.number, a.number2, b.id, b.number3, b.number4, c.id, c.score, (a.number - b.number3) as a_b_difference, (a.number2 - b.number4) as a_b_difference3 FROM file a
INNER JOIN file b on a.id = b.id
INNER JOIN file c on a.id = c.id

我想减去两个字段并将id上的所有三个文件合并。但是,我的结果填充了重复数据。

这是我得到的一个样本:

enter image description here

2 个答案:

答案 0 :(得分:1)

SELECT distinct a.id, a.number, a.number2, b.id, b.number3, b.number4, c.id, c.score, (a.number - b.number3) as a_b_difference, (a.number2 - b.number4) as a_b_difference3 FROM file a
INNER JOIN file b on a.id = b.id
INNER JOIN file c on a.id = c.id

答案 1 :(得分:1)

EntityManagerFactoryc.id,因此不需要a.id

join

至于你的其他问题,我没有看到重复。如果生成的行太多,可能需要修复SELECT a.id, a.number, a.number2, b.id, b.number3, b.number4, a.id, c.score, (a.number - b.number3) as a_b_difference, (a.number2 - b.number4) as a_b_difference3 FROM file a INNER JOIN file b ON a.id = b.id; 条件。如果整行都重复,您可以使用JOIN。如果您想在群组中添加一行,则可以使用SELECT DISTINCT()GROUP BY