选择子查询

时间:2016-02-18 03:54:17

标签: mysql sql

过去一小时我一直在喋喋不休..

我想从我的表clickednumbers

中获取最新数据的TOP 5

clickednumbers只有列numbers作为INT& numberTime作为时间戳

我的查询

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

我总是得到错误

#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

很抱歉打扰:(

目标:根据数字时间

获取最新数字的前5名平均值

2 个答案:

答案 0 :(得分:3)

要获得前5个的平均值,请在FROM子句中使用子查询:

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

答案 1 :(得分:3)

检查this out,了解更多有关正在发生的事情的信息。我认为您的查询需要看起来更像这样:

SELECT AVG(x.numbers) 
  FROM (SELECT numbers FROM clickednumbers ORDER BY numberTime DESC
  LIMIT 5) as x