GROUP or DISTINCT after JOIN returns duplicates

后端 未结 4 1114
深忆病人
深忆病人 2020-12-19 14:01

I have two tables, products and meta. They are in relation 1:N where each product row has at least one meta row via foreign key.

(viz. SQLf

4条回答
  •  失恋的感觉
    2020-12-19 15:01

    Use distinct on as suggested by @Craig's answer but combined with the order by clause as explicated in the comments. SQL Fiddle

    select distinct on(m.product_id) * 
    from
        meta m
        inner join
        products p on p.id = m.product_id
    order by m.product_id, m.id desc;
    

提交回复
热议问题