【问题标题】:Get greatest (or lowest) field in mysql table when join already exists当连接已经存在时,获取 mysql 表中的最大(或最小)字段
【发布时间】:2016-02-15 23:07:32
【问题描述】:

我想为不同字段的特定值获取字段中的最大(或最低)值,但我有点迷茫。我已经知道有关该主题的已回答问题,但我已经加入了我的查询,我无法将找到的出色答案应用于我的具体问题。

我有两张表,分别是寄存器和记录。 Records 每月列出一次所有(天气)台站(每个 stationid 表示 12 次,如果存在完整数据,则一个 stationid 不能出现超过 12 次),并且 register 列出了所有台站及其一些特征。为了这个例子,这两个表看起来很像这样:

CREATE TABLE IF NOT EXISTS `records` (
`stationid` varchar(30),
`month` int(11),
`tmin` decimal(3,1),
`tmax` decimal(3,1),
`commentsmax` text,
`commentsmin` text,
UNIQUE KEY `webcode` (`stationid`,`month`)
);

INSERT INTO `records` (`stationid`, `month`, `tmin`, `tmax`, `commentsmin`, `commentsmax`) VALUES
('station1', 7, '10.0', '46.0', 'Extremely low temperature.', 'Very high temperature.'),
('station2', 7, '15.0', '48.0', 'Very low temperature.', 'Extremely low temperature.'),
('station1', 1, '-10', '15', 'Extremely low temperature.', 'Somewhat high temperature.');

CREATE TABLE IF NOT EXISTS `register` (
`stationid` varchar(30),
`stationname` varchar(40),
`stationowner` varchar(10),
`georegion` varchar(40),
`altitude` int(4),
KEY `stationid` (`stationid`)
);

INSERT INTO `register` (`stationid`, `stationname`, `stationowner`, `georegion`, `altitude`) VALUES
('station1', 'Halifax', 'Maria', 'the North', 16),
('station2', 'Leeds', 'Peter', 'the South', 240);

想要的输出是:

+-------------+-------+-------+---------------+-----------+----------+-----------------------------+
| stationname | month | tmin  |  stationowner | georegion | altitude |        commentsmin          |
+-------------+-------+-------+---------------+-----------+----------+-----------------------------+
| Leeds       |     7 |  15.0 | Peter         | the South |      240 |  Very low temperature       |
| Halifax     |     1 | -10.0 | Maria         | the North |       16 |  Extremely low temperature  |
+-------------+-------+-------+---------------+-----------+----------+-----------------------------+

每个站点仅出现一个表“记录”中温度最低的站点,包括“寄存器”表中的一些站点属性。我正在使用以下代码:

SELECT register.stationname, records.month, min(records.tmin), register.stationowner, register.georegion, register.altitude,  records.commentsmin FROM records INNER JOIN register ON records.stationid=register.stationid GROUP BY records.stationid ORDER BY min(tmin) ASC

但是当表中有很多记录时,它不会给出与最低 tmin 值对应的记录表的正确位 BY stationid。

我在这里看到过类似这样的解决方案:MySQL Greatest N Results with Join Tables,但我无法在我的两张桌子上应用它。如有任何想法,我将不胜感激!

【问题讨论】:

  • 鉴于该数据集,所需的结果集应该是什么样的?

标签: mysql join aggregate-functions greatest-n-per-group


【解决方案1】:
SELECT stuff
  FROM some_table x 
  JOIN some_other_table y 
    ON y.something = x.something
  JOIN 
     ( SELECT something
            , MIN(something_other_thing) min_a 
         FROM that_other_table
        GROUP 
           BY something
     ) z 
    ON z.something = y.something
   AND z.min_a = y.another_thing;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-25
    • 1970-01-01
    • 2020-03-09
    • 1970-01-01
    • 2012-07-06
    • 1970-01-01
    • 1970-01-01
    • 2021-06-02
    相关资源
    最近更新 更多