Answer the question
In order to leave comments, you need to log in
How to get all unique records from the database?
There are 2 tables: products and product_prices , they are linked by the key products.id = product_prices.product_id
And the query:
SELECT DISTINCT ON ( product_prices.product_id) product_id, slug, city_id
FROM"products"
LEFT JOIN "product_prices" ON "product_prices"."product_id" = "products"."id"
ORDER BY city_id asc LIMIT 30
Answer the question
In order to leave comments, you need to log in
distinct on requires using order by on the same fields and some other criteria for determining the "first" row.
If you need some other sorting as a result of the entire query that does not match this one, wrap the query in a subquery and resort as needed again.
select ... from (...) subquery order by ...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question