mySQL按组分列的最大数字

时间:2015-01-19 14:29:06

标签: mysql sql group-by sql-order-by max

我已经搞砸了这段代码很长一段时间了。首先我将我的SQL表设置为char而不是十进制,因为我不希望该数字始终显示十进制值,除非它有一个十进制值(还有另一种方法吗?)。我在代码中将其更改为十进制,但我相信这就是问题所在。我试图为每个物种的每个用户提取最大的鱼,但它没有成功。

SELECT * FROM (SELECT * FROM entries ORDER BY CAST(weight AS DECIMAL(9,3)) DESC) tmp WHERE username = :user GROUP BY species

修改

我已经将我的表改为使用decimal(10,3)而不是char,并且一直在尝试这个公式

SELECT * FROM (SELECT * FROM entries ORDER BY CAST(weight AS INT) DESC) tmp WHERE username = 'BooF' GROUP BY species

但由于某种原因,它会返回此

username    location       species  date    length  weight  timestamp   id
BooF    Muskellunge Lake    Black Crappie   2014-08-31  9.125   0.37    2014-12-20 10:48:06 13
BooF    Black Lake  Largemouth Bass 2014-07-03  16.75   2.62    2014-12-20 10:49:00 2
BooF    Muskellunge Lake    Northern Pike   2014-08-31  32.75   6.86    2014-12-20 10:49:37 14
BooF    Lake Bonaparte  Rock Bass   2014-09-27  7   0.30    2014-12-20 10:50:50 57
BooF    Lake Ozonia Smallmouth Bass 2014-08-15  13  1.19    2014-12-20 10:51:14 1
BooF    Stark Falls Reservoir   Walleye 2014-08-15  16  0.97    2014-12-20 10:51:37 49
BooF    Lake Bonaparte  Yellow Perch    2014-09-27  8.5 0.40    2014-12-20 10:52:01 56

这是我正在使用的表格

username  location      species     date        length  weight  timestamp id
BooF    Lake Ozonia Smallmouth Bass 2014-08-15  13.000  1.190   2014-12-20 10:51:14 1
BooF    Black Lake  Largemouth Bass 2014-07-03  16.750  2.620   2014-12-20 10:49:00 2
BooF    Muskellunge Lake    Largemouth Bass 2014-08-31  12.000  1.000   2014-08-31 22:04:42 7
BooF    Muskellunge Lake    Largemouth Bass 2014-08-31  16.000  2.000   2014-08-31 22:04:42 8
BooF    Muskellunge Lake    Largemouth Bass 2014-08-31  14.000  1.000   2014-08-31 22:04:42 9
BooF    Muskellunge Lake    Largemouth Bass 2014-08-31  16.000  2.000   2014-08-31 22:04:42 10
BooF    Muskellunge Lake    Largemouth Bass 2014-08-31  14.000  2.000   2014-08-31 22:04:42 11
BooF    Muskellunge Lake    Largemouth Bass 2014-08-31  16.000  2.000   2014-08-31 22:05:53 12
BooF    Muskellunge Lake    Black Crappie   2014-08-31  9.125   0.370   2014-12-20 10:48:06 13
BooF    Muskellunge Lake    Northern Pike   2014-08-31  32.750  6.860   2014-12-20 10:49:37 14
BooF    Narrow Lake Northern Pike   2014-03-15  20.000  2.000   2014-09-01 11:08:21 15
BooF    Narrow Lake Largemouth Bass 2014-03-15  14.000  1.000   2014-09-01 11:08:21 16
BooF    Butterfield Lake    Largemouth Bass 2014-05-26  19.000  3.000   2014-09-01 11:08:21 17
BooF    Butterfield Lake    Largemouth Bass 2014-05-26  17.000  2.000   2014-09-01 11:08:21 18
BooF    Red Lake    Northern Pike   2014-06-21  22.000  2.000   2014-09-01 11:08:21 19
BooF    Black Lake  Largemouth Bass 2014-07-03  15.000  2.000   2014-09-01 11:12:08 20
BooF    Black Lake  Largemouth Bass 2014-07-03  15.000  2.000   2014-09-01 11:12:08 21
BooF    Black Lake  Largemouth Bass 2014-07-02  17.000  2.000   2014-09-01 11:12:08 22
BooF    Black Lake  Largemouth Bass 2014-07-01  15.000  2.000   2014-09-01 11:12:08 23
BooF    Black Lake  Largemouth Bass 2014-07-01  15.000  2.000   2014-09-01 11:12:08 24
BooF    Black Lake  Largemouth Bass 2014-06-30  19.250  4.100   2014-12-20 10:53:17 25
BooF    Black Lake  Northern Pike   2014-06-29  26.750  3.940   2014-12-20 10:52:38 26
BooF    Stark Falls Reservoir   Walleye 2014-08-15  16.000  0.970   2014-12-20 10:51:37 49
BooF    Lake Bonaparte  Yellow Perch    2014-09-27  8.500   0.400   2014-12-20 10:52:01 56
BooF    Lake Bonaparte  Rock Bass   2014-09-27  7.000   0.300   2014-12-20 10:50:50 57

正如你在查询后看到的那样,它为重量为2.62磅的黑色湖泊发布了一个大嘴鲈鱼,但是在我的数据表中它显示了4.1的最大低音,所以这就是我被困住的地方。

1 个答案:

答案 0 :(得分:1)

一般来说,子查询中的ORDER BY没有任何意义。 (仅当与FETCH FIRST / LIMIT / TOP等结合使用时才会这样做。)

解决方案是使用相关的子查询来查找“主查询”的当前行的用户名,位置,物种组合中最重的鱼。如果是平局,则返回两行。

SELECT *
FROM entries e1
WHERE username = :user
  AND CAST(weight AS DECIMAL(9,3)) = (select max(CAST(weight AS DECIMAL(9,3)))
                                      from entries e2
                                      where e1.username = e2.username
                                        and e1.location = e2.location
                                        and e1.species = e2.species)

请注意,对于重量而言,char仍然是一个糟糕的选择,因为在比较值时必须投射双方。在表格中回到小数点!