MySQL max() - 不返回 max() 值
MySQL max() - not returning max() value
我有一个查询,我想 return idevent
中给定传感器 ID (sensorID
) 范围内的最高 ID 值,但是查询不是 return取最高值。
当我运行查询减去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)
为什么这不是 return最新值?
用作 , 使用 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
这可以通过 order by limit
完成,因此您只需要在给定的第一个查询
末尾添加以下内容
order by idevent desc limit 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;
到这里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);
我有一个查询,我想 return idevent
中给定传感器 ID (sensorID
) 范围内的最高 ID 值,但是查询不是 return取最高值。
当我运行查询减去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)
为什么这不是 return最新值?
用作 , 使用 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
这可以通过 order by limit
完成,因此您只需要在给定的第一个查询
order by idevent desc limit 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;
到这里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);