Answer the question
In order to leave comments, you need to log in
How to get rows from JOIN if DISTINCT is specified in PostgreSQL?
Hello!
Let's say:
1. There is a book table with fields: id , book_name ;
2. There is a table of authors authors with fields: id , book_id , author_name .
3. A book can have multiple authors.
You need to get let's say 10 books and authors to them.
I make a request something like:
SELECT DISTINCT * FROM books INNER JOIN authors ON (books.id=authors.book_id) LIMIT 10
Answer the question
In order to leave comments, you need to log in
It is better, of course, to break this into two queries: books and authors of all selected books via book_id IN (x, y, z).
What you want can be done with one complex query, but for each author, the information about the book will be duplicated - why drive too much over the network?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question