根据多列选择最大值列行

时间:2016-02-20 12:57:25

标签: mysql sql

我有下表documents

+--------+-------------+------------+----------+---------+
| doc_id | module_name | mapping_id | doc_type | version |
+--------+-------------+------------+----------+---------+
|      5 | asdf        | asd        | POI      |       1 |
|      6 | asdf        | asd        | POI      |       2 |
|      7 | asdf        | asd        | CAF      |       1 |
|      8 | asdf        | abc        | POI      |       1 |
|      9 | asdf        | abc        | ISR      |       1 |
|     10 | asdf        | abc        | ISR      |       2 |
|     11 | asdf        | xyz        | POA      |       1 |
|     12 | asdf        | xyz        | CAF      |       1 |
|     13 | asdf        | xyz        | CAF      |       2 |
|     14 | asdf        | xyz        | CAF      |       3 |
|     15 | pqrs        | asd        | POI      |       1 |
|     16 | pqrs        | asd        | POI      |       2 |
|     17 | pqrs        | asd        | CAF      |       1 |
|     18 | pqrs        | abc        | POI      |       1 |
|     19 | pqrs        | abc        | ISR      |       1 |
|     20 | pqrs        | abc        | ISR      |       2 |
|     21 | pqrs        | xyz        | POA      |       1 |
|     22 | pqrs        | xyz        | CAF      |       1 |
|     23 | pqrs        | xyz        | CAF      |       2 |
|     24 | pqrs        | xyz        | CAF      |       3 |
+--------+-------------+------------+----------+---------+

我希望得到结果:

+--------+-------------+------------+----------+---------+
| doc_id | module_name | mapping_id | doc_type | version |
+--------+-------------+------------+----------+---------+
|      6 | asdf        | asd        | POI      |       2 |
|      7 | asdf        | asd        | CAF      |       1 |
|      8 | asdf        | abc        | POI      |       1 |
|     10 | asdf        | abc        | ISR      |       2 |
|     11 | asdf        | xyz        | POA      |       1 |
|     14 | asdf        | xyz        | CAF      |       3 |
|     16 | pqrs        | asd        | POI      |       2 |
|     17 | pqrs        | asd        | CAF      |       1 |
|     18 | pqrs        | abc        | POI      |       1 |
|     20 | pqrs        | abc        | ISR      |       2 |
|     21 | pqrs        | xyz        | POA      |       1 |
|     24 | pqrs        | xyz        | CAF      |       3 |
+--------+-------------+------------+----------+---------+

max版本号行应与module_namemapping_iddoc_type一起返回。

我无法弄清楚查询。需要帮助。

2 个答案:

答案 0 :(得分:1)

在大多数数据库中,您将使用setTimeout(function() { document.getElementById("focusme").focus(); }, 1); 。在MySQL中,这不可用。一种方法是row_number()join

group by

如果我们从字面上理解OP的评论,或者可能只是

select t.*
from t join
     (select module_name, mapping_id, doc_type, max(version) as version
      from t
      group by module_name, mapping_id, doc_type
     ) tt
     using (module_name, mapping_id, doc_type, version);

答案 1 :(得分:1)

另一种选择

select *
from documents d
where not exists
  (
    select 1 from documents dv
    where dv.doc_type = d.doc_type
      and dv.version > d.version
  )

字面意思:"相同值没有更大的版本号"