简体   繁体   English

MySQL根据列的最大值返回不同的行

[英]MySQL Return Distinct rows based on highest value of column

So I have 2 tables in MySQL (MariaDB). 所以我在MySQL(MariaDB)中有2个表。 One has all unique data per row, the other has the version history of the first tables unique data. 一个具有每行所有唯一数据,另一个具有第一个表的唯一版本的历史记录数据。 There is only one key between the 2, GeoID. 两个GeoID之间只有一个键。

The table (t1) with unique ID and GeoID 具有唯一ID和GeoID的表(t1)

ID , GeoID , ColumnA , ColumnB ID,GeoID,ColumnA,ColumnB

This table (t2) has the version history. 该表(t2)具有版本历史记录。

ID , VersionID , GeoID , TerrainID , Action ID,VersionID,GeoID,TerrainID,操作

So I am doing a LEFT JOIN on t2 where the GeoID matches in both tables and action = 4. The catch is, because t2 is a version table, it can return multiple rows where the GeoID matches, showing all versions. 因此,我在t2上进行了LEFT JOIN操作,其中两个表中的GeoID都匹配,并且action =4。要注意的是,因为t2是版本表,它可以返回与GeoID匹配的多行,显示所有版本。 I need the latest version of the data from t2 joined with t1, and from t1 I only want rows from ColumnA that are 0 我需要从t2到t1的最新数据,从t1开始,我只希望ColumnA中的行为0

Lets have some values of 让我们有一些值

     t1
     ID | GeoID | ColumnA | ColumnB
     1  |  1000 |    0    |  42
     2  |  4387 |    3    |  432
     3  |  9573 |    0    |  NULL

     t2
     ID | VersionID | GeoID | TerrainID | Action
     1  |     1     |  1000 |    221    |   4
     2  |     2     |  1000 |    313    |   2
     3  |     1     |  4387 |    541    |   4
     4  |     1     |  9573 |    329    |   4
     5  |     3     |  1000 |    323    |   4
     6  |     2     |  9573 |    423    |   1

Now, what I need returned are the rows are the JOINed values on GeoID, the associated TerrainID from t2 and only the values from t2 where the version is the MAX for that geoid row. 现在,我需要返回的是行,它们是GeoID上的JOINed值,来自t2的关联TerrainID以及仅来自t2的值,其中版本是该大地水准面行的最大值。

So it should look like this, table above column denotes where the data comes from. 因此,它看起来应该像这样,表格上方的列表示数据的来源。

       t1        t2        t2
     GeoID | TerrainID | Version
      1000 |    323    |   3
      9573 |    423    |   2

Doing the LEFT OUTER JOIN against a GROUPed version of the t2 table should do the trick: 对t2表的GROUPED版本进行LEFT OUTER JOIN应该可以解决这个问题:

SELECT t1.GeoID, t2_grouped.VersionID, t2_grouped.TerrainID
FROM t1
LEFT OUTER JOIN (SELECT GeoID, TerrainID, MAX(VersionID) AS VersionID FROM t2 GROUP BY GeoID, TerrainID) t2_grouped
  ON t2_grouped.GeoId = t1.GeoID

WHERE t1.ColumnA = 0

Please try: 请试试:

SELECT
   t1.GeoID,
   t2_lastver.TerrainID,
   t2_lastver.VersionID,
   t2_lastver.Action
FROM
   t1
   LEFT OUTER JOIN
   (
      SELECT
         t2.*
      FROM
         t2
      WHERE
         t2.ID in (SELECT MAX(t2_max.ID) FROM t2 AS t2_max WHERE t2_max.GeoID = t2.GeoID)
   ) AS t2_lastver
      ON t2_lastver.GeoID = t1.GeoID
WHERE 
   t1.ColumnA = 0;

Here you can find an SQL Fiddle illustrating the query. 在这里,您可以找到说明查询的SQL Fiddle

Hope it helps. 希望能帮助到你。

Try this: 尝试这个:

SELECT 
    t1.GeoID,
    t2.TerrainID,
    t.Version
FROM t1
LEFT JOIN (
    SELECT
        GeoID,
        Version = MAX(VersionID)
    FROM t2
    WHERE Action = 4
    GROUP BY GeoID
) AS t 
    ON t.GeoID = t1.GeoID
LEFT JOIN t2
    ON t2.GeoID = t.GeoID
    AND t2.VersionId = t.Version
WHERE
    t1.ColumnA = 0

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

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