【问题标题】:MySQL ERROR 1241 (21000): Operand should contain 1 column(s) on Aggregate QueryMySQL ERROR 1241 (21000):操作数应在聚合查询中包含 1 列
【发布时间】:2016-11-05 16:35:59
【问题描述】:

我正在使用 MySQL Ver 14.14 Distrib 5.5.52,用于 debian-linux-gnu (armv7l),使用 readline 6.3 在 raspbian OS 和 RaspberyPI 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);

任何想法我忽略了什么?

谢谢 史蒂夫

【问题讨论】:

  • 从您的 GROUP BY 中删除 ()
  • 成功了!!!谢谢@Mihai!!!
  • @Mihai,为什么不将其发布为答案?

标签: mysql sql


【解决方案1】:

Mihai's answer 工作:

从您的GROUP BY 中删除()

【讨论】:

    猜你喜欢
    • 2013-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    相关资源
    最近更新 更多