Select average from MySQL table with LIMIT

前端 未结 2 1845
别那么骄傲
别那么骄傲 2020-12-16 10:50

I am trying to get the average of the lowest 5 priced items, grouped by the username attached to them. However, the below query gives the average price for each user (which

相关标签:
2条回答
  • 2020-12-16 11:02

    I think this is what you're after:

    SELECT AVG(items.price)
      FROM (SELECT t.price
              FROM TABLE t
             WHERE t.price > '0' 
               AND t.item_id = '$id'
          ORDER BY t.price
             LIMIT 5) items
    

    It will return the average of the 5 lowest prices - a single answer.

    0 讨论(0)
  • 2020-12-16 11:10

    Simple solution below.

    Query:

    SELECT  AVG(Column_name) 
    FROM  (SELECT Column_name 
    FROM  Table
    WHERE  ColumnID < number[Limit you want] )
    
    0 讨论(0)
提交回复
热议问题