How to get the 3 data that have the largest column value in the table

I use knex and a bookshelf, and my table consists of the author, title, content, count, and each information looks like this:

author: 'John Doe',
title: 'aaaaa',
content: 'aaaaaaaa'
count: 54,

I want to get data based on count value, and I want to get 4 data with the highest count value.

If I want to get all the data, I do like this:

router.get('/', (req, res) => {
   Article.forge().fetchAll().then(article => {
      res.json(article);
   })
}) 

Is there a way that I can do like forge({ count: 3 data that has the highest count value })or
What should I add code so that I can do this?

+4
source share
1 answer

Combine orderBy with fetchPage

Article
  .orderBy('-count')
  .fetchPage({
    pageSize: 3
  })
  .forge()

, . , ORM. - knex:

  knex('articles')
    .orderBy('count', 'desc')
    .limit(3)

, , .. rows[0].id, rows[0].get('id')

+4

Source: https://habr.com/ru/post/1654431/


All Articles