BerlinElle
BerlinElle

Reputation: 15

MySQL query add limit on relation table

I have 2 tables linked with OneToMany. Table books and table authors. Each author has many books. I want to fetch a list of books, but for each author, only 10 books.

How to achieve that?

Thanks for any help

Upvotes: 0

Views: 38

Answers (1)

The Impaler
The Impaler

Reputation: 48770

You can use ROW_NUMBER() for example:

select *
from (
  select *, row_number() over(partition by author_id order by title) as rn
  from book
) x
where rn <= 10

Upvotes: 1

Related Questions