mySQL 最大组数

mySQL Largest number by group

我弄乱这段代码已经有一段时间了。首先,我将 SQL table 设置为 char 而不是 decimal,因为我不希望该数字始终显示十进制值,除非它具有十进制值(是否有另一种方法可以做到这一点? ).我在我的代码中将其更改为十进制,但我相信这就是问题所在。我正在尝试为每个用户每个物种拉最大的鱼,但没有成功。

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

编辑

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

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

但出于某种原因 returns 这个

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

这是我正在使用的table

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 磅的黑湖大口黑鲈,但根据我的数据 sheet 它显示最大的黑鲈为 4.1,所以这就是我被困的地方。

一般来说,子查询中的 ORDER BY 是没有意义的。 (它仅在与 FETCH FIRST/LIMIT/TOP 等结合使用时起作用)

解决方案是使用相关子查询为 "main query" 的当前行的用户名、位置、物种组合找到最重的鱼。如果是平局,将返回两行。

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 表示重量仍然是一个糟糕的选择,因为在比较值时您必须转换双方。在 table!

中返回十进制