SELECT DISTINCT a.s_id, select2Result.s_id, select2Result."mNrPhone",
       select2Result."dNrPhone"
FROM "Table1" AS a INNER JOIN
    (
    SELECT b.s_id, c."mNrPhone", c."dNrPhone" FROM "Table2" AS b, "Table3" AS c
    WHERE b.a_id = 1001 AND b.s_id = c.s_id
    ORDER BY b.last_name) AS select2Result
ON a.a_id = select2Result.student_id
WHERE a.k_id = 11211

它返回:
1001;1001;"";""
1002;1002;"";""
1002;1002;"2342342232123";"2342342"
1003;1003;"";""
1004;1004;"";""

1002值重复了两次,但不应该重复,因为我使用了DISTINCT并且没有其他表的id重复了两次。

最佳答案

您可以像这样使用DISTINCT ON

   SELECT DISTINCT ON (a.s_id)
          a.s_id, select2Result.s_id, select2Result."mNrPhone",
          select2Result."dNrPhone"
   ...

但就像其他人告诉你的,“重复记录”确实不同。

关于postgresql - 在Postgres中选择非重复值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11900477/

10-13 08:47