Join one to many and retrieve single result

前端 未结 3 787
北恋
北恋 2021-02-13 03:09

I have two tables, in PostgreSQL if that matters, with one to many relations. I need to join them so that for each \"one\" I only get single result from the \"many\" table. Not

3条回答
  •  青春惊慌失措
    2021-02-13 03:55

    Here's what I'd do on SQL Server.

    SELECT a.ID,
        a.NAME,
        a.DATE,
        b.CODE1,
        b.CODE2
    FROM TABLE_A a
    JOIN TABLE_B b
        on a.ID = b.ID
    WHERE b.SORT = (SELECT MIN(SORT) 
        FROM TABLE_B
        WHERE ID = b.ID)
    

提交回复
热议问题