MySQL 错误 1241 (21000) : Operand should contain 1 column(s) on Aggregate Query

标签 mysql sql

我使用的是 MySQL Ver 14.14 Distrib 5.5.52,适用于 debian-linux-gnu (armv7l),使用在 RaspberyPI 3 上运行的 raspbian 操作系统上的 readline 6.3。

我运行以下查询并得到以下结果。

mysql> SELECT
    -> sensorMeasurements.sensorMeasurementDate,
    -> MIN(sensorMeasurements.sensormeasurementvalue) as "minValue",
    -> MAX(sensorMeasurements.sensormeasurementvalue) as "maxValue",
    -> AVG(sensorMeasurements.sensormeasurementvalue) as "avgValue"
    -> FROM   sensorMeasurements
    ->        INNER JOIN userSystemSensors
    ->                ON sensorMeasurements.usersystemsensorid =
    ->                   userSystemSensors.usersystemsensorid
    ->        INNER JOIN sensors
    ->                ON userSystemSensors.sensorid = sensors.sensorid
    ->        INNER JOIN userSystems
    ->                ON userSystemSensors.usersystemid = userSystems.usersystemid
    ->        INNER JOIN users
    ->                ON userSystems.userid = users.userid
    -> WHERE  sensors.sensorid = 1
    ->        AND sensorMeasurements.sensormeasurementdatetime BETWEEN
    ->            "2016-11-05 10:00:00" AND "2016-11-15 11:00:00"
    -> GROUP BY (sensorMeasurements.sensorMeasurementDate);

+-----------------------+----------+----------+-------------------+ 
| sensorMeasurementDate | minValue | maxValue | avgValue          |
+-----------------------+----------+----------+-------------------+ 
| 2016-11-05            |     66.9 |       73 | 69.56107784431138 | 
| 2016-11-06            |     51.5 |     73.4 | 64.03751600512162 | 
| 2016-11-07            |     40.3 |     68.7 | 58.34236111111102 | 
| 2016-11-08            |     39.2 |     59.4 |  48.1092361111111 | 
| 2016-11-09            |     13.2 |     54.8 | 36.78529850746269 |
+-----------------------+----------+----------+-------------------+

当我将该查询修改为以下内容时,出现错误 错误 1241 (21000):操作数应包含 1 列

SELECT 
sensors.sensorName,
sensorMeasurements.sensorMeasurementDate,
MIN(sensorMeasurements.sensormeasurementvalue) as "minValue",
MAX(sensorMeasurements.sensormeasurementvalue) as "maxValue",
AVG(sensorMeasurements.sensormeasurementvalue) as "avgValue"
FROM   sensorMeasurements 
       INNER JOIN userSystemSensors 
               ON sensorMeasurements.usersystemsensorid = 
                  userSystemSensors.usersystemsensorid 
       INNER JOIN sensors 
               ON userSystemSensors.sensorid = sensors.sensorid 
       INNER JOIN userSystems 
               ON userSystemSensors.usersystemid = userSystems.usersystemid 
       INNER JOIN users 
               ON userSystems.userid = users.userid 
WHERE  sensors.sensorid = 1 
       AND sensorMeasurements.sensormeasurementdatetime BETWEEN 
           "2016-11-05 10:00:00" AND "2016-11-15 11:00:00"
GROUP BY (sensors.sensorName, sensorMeasurements.sensorMeasurementDate);

我忽略了什么想法吗?

谢谢 史蒂夫

最佳答案

Mihai's answer工作过:

Remove the () from your GROUP BY

关于MySQL 错误 1241 (21000) : Operand should contain 1 column(s) on Aggregate Query,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40440632/

相关文章:

查询与子查询中聚合顺序之间的 MySQL 差异

MySQL随机排序并保存在数据库中,无需ID

php - WP环境下内连接SQL,where id=id

java - 如何将 java.util.Date 转换为时间戳中的当前时间..?

MySQL 字符串函数

python - MYSQL 语句中的动态表名 (Google Cloud SQL)

单个数字的 MySQL 数据类型 -tinyint(1) 或smallint(1) 或int(1)

php - 查询并解码mysql数据库中的json

mysql - SQL,使用数学将行转换为列

sql - 内部查询和连接