F
F
furyon2016-04-30 10:06:24
PostgreSQL
furyon, 2016-04-30 10:06:24

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

I will get books and one author each, but I need all the authors, is it possible to do this with one request without additional ones for each book?
I don't even know how to google it.
Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Ukolov, 2016-04-30
@furyon

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 question

Ask a Question

731 491 924 answers to any question