Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting a Subquery

Tags:

sql

mysql

I've been bugging this for the past hour..

I want to get the TOP 5 from latest data from my table clickednumbers

clickednumbers has only to columns numbers as INT & numberTime as timestamp

My query

SELECT AVG( SELECT *
            FROM clickednumbers
            ORDER BY numberTime DESC
            LIMIT 5)
FROM clickednumbers

and im always getting the error

#1064 - You have an error in your SQL syntax;check the manual that corresponds to your MariaDB server version for the right syntanx to use near 'SELECT * FROM clicked numbers ORDER BY numberTime DESC ' at line 1

MariaDB Version:Server type: MariaDB Server version: 10.1.9-MariaDB - mariadb.org binary distribution Protocol version: 10

Sorry for bothering :(

Goal : To get the average of top 5 from latest numbers based on the numbersTime

like image 557
Kiel Avatar asked Jan 26 '26 18:01

Kiel


1 Answers

To get the average of the top 5, use a subquery in the FROM clause:

SELECT AVG(numbers)
FROM (SELECT *
      FROM clickednumbers
      ORDER BY numberTime DESC
      LIMIT 5
     ) cn;
like image 77
Gordon Linoff Avatar answered Jan 28 '26 08:01

Gordon Linoff



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!