MySQL max() - 不返回max()值

时间:2015-03-09 10:51:28

标签: mysql sql database max

我有一个查询,我想在idevent中返回给定范围内的传感器ID(sensorID)中的最高id值但是查询没有返回最高值。

运行查询时减去max()声明的结果:

mysql> SELECT * FROM events WHERE timestamp BETWEEN "2015-03-09 10:45:35" - INTERVAL 4000 SECOND AND "2015-03-09 10:45:35" AND (sensorID = 34035434 OR sensorID = 34035492 OR sensorID = 34035426 OR sensorID = 34035482 OR sensorID = 34035125 OR sensorID = 34035498 OR sensorID = 34035508 OR sensorID = 34035444 OR sensorID = 34035418 OR sensorID = 34035466 OR sensorID = 34035128 OR sensorID = 34035119 OR sensorID = 34035448 OR sensorID = 34037294 OR sensorID = 34035549);
+---------+---------------------+----------+-------+
| idevent | timestamp           | sensorID | event |
+---------+---------------------+----------+-------+
|  117794 | 2015-03-09 10:14:58 | 34035434 |     9 |
|  117795 | 2015-03-09 10:15:03 | 34035508 |     9 |
|  117796 | 2015-03-09 10:15:17 | 34035508 |     1 |
+---------+---------------------+----------+-------+
3 rows in set (0.00 sec)

max()语句添加为HAVING子句时给出的结果:

mysql> SELECT * FROM events WHERE timestamp BETWEEN "2015-03-09 10:45:35" - INTERVAL 4000 SECOND AND "2015-03-09 10:45:35" AND (sensorID = 34035434 OR sensorID = 34035492 OR sensorID = 34035426 OR sensorID = 34035482 OR sensorID = 34035125 OR sensorID = 34035498 OR sensorID = 34035508 OR sensorID = 34035444 OR sensorID = 34035418 OR sensorID = 34035466 OR sensorID = 34035128 OR sensorID = 34035119 OR sensorID = 34035448 OR sensorID = 34037294 OR sensorID = 34035549) HAVING max(idevent);
+---------+---------------------+----------+-------+
| idevent | timestamp           | sensorID | event |
+---------+---------------------+----------+-------+
|  117794 | 2015-03-09 10:14:58 | 34035434 |     9 |
+---------+---------------------+----------+-------+
1 row in set (0.00 sec)

为什么这不会返回最新值?

4 个答案:

答案 0 :(得分:1)

使用as,使用order by

SELECT * FROM events WHERE timestamp BETWEEN "2015-03-09 10:45:35" - INTERVAL 4000 SECOND AND "2015-03-09 10:45:35" AND (sensorID = 34035434 OR sensorID = 34035492 OR sensorID = 34035426 OR sensorID = 34035482 OR sensorID = 34035125 OR sensorID = 34035498 OR sensorID = 34035508 OR sensorID = 34035444 OR sensorID = 34035418 OR sensorID = 34035466 OR sensorID = 34035128 OR sensorID = 34035119 OR sensorID = 34035448 OR sensorID = 34037294 OR sensorID = 34035549)
 order by idevent desc

答案 1 :(得分:1)

可以使用order by limit完成此操作,因此您只需在给定的第一个查询结尾处添加以下内容

order by idevent desc limit 1

答案 2 :(得分:1)

我认为你需要这个查询:

SELECT MAX(idevent) FROM events WHERE timestamp BETWEEN "2015-03-09 10:45:35" - INTERVAL 4000 SECOND AND "2015-03-09 10:45:35" AND sensorID IN ('34035434', '34035492', '34035426', '34035482', '34035125', '34035498', '34035508', '34035444', '34035418', '34035466', '34035128', '34035119', '34035448', '34037294', '34035549') GROUP BY idevent;

答案 3 :(得分:0)

到这里http://www.w3schools.com/sql/sql_func_max.asp

查询错误。

试试这个:

SELECT MAX(Price) FROM events WHERE timestamp BETWEEN "2015-03-09 10:45:35" - INTERVAL 4000 SECOND AND "2015-03-09 10:45:35" AND (sensorID = 34035434 OR sensorID = 34035492 OR sensorID = 34035426 OR sensorID = 34035482 OR sensorID = 34035125 OR sensorID = 34035498 OR sensorID = 34035508 OR sensorID = 34035444 OR sensorID = 34035418 OR sensorID = 34035466 OR sensorID = 34035128 OR sensorID = 34035119 OR sensorID = 34035448 OR sensorID = 34037294 OR sensorID = 34035549);