MySQL - 带有日期的多个MAX值

时间:2012-12-22 10:19:11

标签: mysql select max

我有表结构:

A | B | C |日期

我想选择MAX(A),MAX(B),MAX(C)和每个MAX赋值日期...可以在MySQL中只用1个SELECT(没有像SELECT * FROM这样的时态表) SELECT ....))?

示例:

A  | B  | C  | Date
------------------------
10 | 20 | 0  | 25.5.2012
0  | 13 | 7  | 25.3.2012
70 | 5  | 15 | 25.4.2012

Result:
A -> 70 - 25.4.2012
B -> 20 - 25.5.2012
C -> 15 - 25.4.2012

2 个答案:

答案 0 :(得分:1)

  ( SELECT 'A' AS ColumnM
          , a  AS Result
          , Date
    FROM tableX  
    ORDER BY a DESC
        LIMIT 1 
  )
UNION ALL
  ( SELECT 'B'
          , b 
          , Date
    FROM tableX  
    ORDER BY b DESC
        LIMIT 1 
  )
UNION ALL
  ( SELECT 'C'
          , c 
          , Date
    FROM tableX  
    ORDER BY c DESC
        LIMIT 1 
  ) ;

答案 1 :(得分:0)

我认为你需要这个::

Select
MAX(a),
MAX(b),
MAX(c)
from table
GROUP BY `Date`