简体   繁体   English

Mysql max()和min()之间的区别?

[英]Mysql difference between max() and min()?

I got a problem with a mySql query and max() function. 我的mySql查询和max()函数遇到问题。 If I do : 如果我做 :

Select * from Data group by experiment having min(timestamp)

This query return what I want, and correct value. 此查询返回我想要的,并纠正值。 I got this : 我懂了 :

+----------+---------+----------+---------------------+----------------+------------+
| id       | mote_id | label_id | timestamp           | value          | experiment |
+----------+---------+----------+---------------------+----------------+------------+
|  3768806 |      10 |       30 | 2014-04-22 14:37:07 |              0 |         13 |
| 10989209 |      12 |       22 | 2014-04-25 10:44:03 | 2.532958984375 |         15 |
| 11943537 |       6 |       19 | 2014-05-05 17:20:15 |           1228 |         16 |
| 12042549 |      16 |       26 | 2014-05-06 10:48:59 |          22.86 |         17 |
| 12176642 |      15 |       23 | 2014-05-07 15:19:35 |              0 |         18 |
| 12195344 |      10 |        6 | 2014-05-07 15:27:23 |           3460 |         19 |
| 12222470 |      15 |        8 | 2014-05-07 15:38:38 |              1 |         21 |
| 12343934 |      10 |       19 | 2014-05-12 10:35:42 |            742 |         23 |
+----------+---------+----------+---------------------+----------------+------------+

But, if i do : 但是,如果我这样做:

Select * from Data group by experiment having max(timestamp)

This query return wrong values... like this : 该查询返回错误的值...就像这样:

+----------+---------+----------+---------------------+----------------+------------+
| id       | mote_id | label_id | timestamp           | value          | experiment |
+----------+---------+----------+---------------------+----------------+------------+
|  3768806 |      10 |       30 | 2014-04-22 14:37:07 |              0 |         13 |
| 10989209 |      12 |       22 | 2014-04-25 10:44:03 | 2.532958984375 |         15 |
| 11943537 |       6 |       19 | 2014-05-05 17:20:15 |           1228 |         16 |
| 12042549 |      16 |       26 | 2014-05-06 10:48:59 |          22.86 |         17 |
| 12176642 |      15 |       23 | 2014-05-07 15:19:35 |              0 |         18 |
| 12195344 |      10 |        6 | 2014-05-07 15:27:23 |           3460 |         19 |
| 12222470 |      15 |        8 | 2014-05-07 15:38:38 |              1 |         21 |
| 12343934 |      10 |       19 | 2014-05-12 10:35:42 |            742 |         23 |
+----------+---------+----------+---------------------+----------------+------------+

In the first query, if I replace min(timestamp) by timestamp=min(timestamp) , it works, but in the second, "timestamp=max(timestamp)" return nothing 在第一个查询中,如果我将min(timestamp)替换为timestamp=min(timestamp) ,则可以使用,但是在第二个查询中, "timestamp=max(timestamp)"返回任何内容

Finally, Select experiment,max(timestamp) return correct values. 最后, Select experiment,max(timestamp)返回正确的值。

mysql> select *,max(timestamp) from Data group by experiment; mysql>通过实验从数据组中选择*,max(timestamp);

+----------+---------+----------+---------------------+----------------+------------+---------------------+
| id       | mote_id | label_id | timestamp           | value          | experiment | max(timestamp)      |
+----------+---------+----------+---------------------+----------------+------------+---------------------+
|  3768806 |      10 |       30 | 2014-04-22 14:37:07 |              0 |         13 | 2014-04-24 16:03:29 |
| 10989209 |      12 |       22 | 2014-04-25 10:44:03 | 2.532958984375 |         15 | 2014-05-05 10:34:35 |
| 11943537 |       6 |       19 | 2014-05-05 17:20:15 |           1228 |         16 | 2014-05-06 10:35:15 |
| 12042549 |      16 |       26 | 2014-05-06 10:48:59 |          22.86 |         17 | 2014-05-07 15:19:33 |
| 12176642 |      15 |       23 | 2014-05-07 15:19:35 |              0 |         18 | 2014-05-07 15:27:23 |
| 12195344 |      10 |        6 | 2014-05-07 15:27:23 |           3460 |         19 | 2014-05-07 15:38:01 |
| 12222470 |      15 |        8 | 2014-05-07 15:38:38 |              1 |         21 | 2014-05-07 16:30:38 |
| 12343934 |      10 |       19 | 2014-05-12 10:35:42 |            742 |         23 | 2014-05-14 09:25:44 |
+----------+---------+----------+---------------------+----------------+------------+---------------------+

I know I can make a subquery to solve my probleme, but the tables contains thousands rows, and this solution is too long... 我知道我可以进行子查询来解决我的问题,但是表包含数千行,并且此解决方案太长了...

Ps : I can't use Select*, max(timestamp) even if it works because the query is run by EJB in JEE. Ps:即使查询有效Select*, max(timestamp)我也无法使用Select*, max(timestamp) ,因为查询是由JEE中的EJB运行的。

You select not determined values grouped by field experiment . 您可以选择未确定的值,这些值是通过现场experiment分组的。 No one can give you a guarantee that non-agregated fields would correspond to MIN or MAX values of some aggregated field. 没有人可以保证非聚合字段将与某个聚合字段的MIN或MAX值相对应。

You HAVE TO use sub-query or self-join to get the right records. 您必须使用子查询或自联接来获取正确的记录。

See more here: http://dev.mysql.com/doc/refman/5.6/en/example-maximum-column-group-row.html 在此处查看更多信息: http : //dev.mysql.com/doc/refman/5.6/en/example-maximum-column-group-row.html

The HAVING clause expects a boolean expression. HAVING子句需要一个布尔表达式。 In other DBMS your code sample would trigger an error. 在其他DBMS中,您的代码示例将触发错误。 In MySQL, you'll get the expression cast to boolean: 在MySQL中,您将表达式转换为布尔值:

  • Zero → false 零→假
  • Non-zero → true 非零→真

And since your expression is constant for the whole set, it won't filter out partial rows. 而且由于您的表达式对于整个集合是恒定的,因此不会过滤掉部分行。

As about this: 至于这个:

HAVING timestamp = max(timestamp)

The HAVING clause evaluates after WHERE and GROUP BY . HAVING子句 WHEREGROUP BY 之后求值。 At that point, using individual row values of the timestamp column doesn't make any sense. 那时,使用timestamp列的各个行值没有任何意义。 As usual, MySQL allows that but you must take into account that: 与往常一样, MySQL允许这样做,但您必须考虑到以下几点:

In standard SQL, a query that includes a GROUP BY clause cannot refer to nonaggregated columns in the HAVING clause that are not named in the GROUP BY clause. 在标准SQL中,包含GROUP BY子句的查询不能引用HAVING子句中未在GROUP BY子句中命名的未聚合列。 A MySQL extension permits references to such columns to simplify calculations. MySQL扩展允许对此类列的引用以简化计算。 This extension assumes that the nongrouped columns will have the same group-wise values. 此扩展假定未分组的列将具有相同的按组值。 Otherwise, the result is indeterminate. 否则,结果不确定。

In other words, your results are arbitrary (not even random). 换句话说,您的结果是任意的(甚至不是随机的)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM