Mysql,选择max和match值

时间:2012-06-26 22:36:01

标签: mysql

我有一张包含日期和值的表格。 我要做的是获取值的MAX()值和与该值对应的日期。 即。

+------------+-------+
| pdate      | score |
+------------+-------+
| 2012-05-01 |    80 |
| 2012-05-02 |    50 |
| 2012-05-03 |    52 |
| 2012-04-02 |   100 |
| 2012-05-02 |    10 | 
+------------+-------+

我想要的输出是2012-04-02 - 100

这是我的疑问:

SELECT pdate,MAX(Score) as maxscore FROM tblpulse 
 WHERE DID = '171488' && pdate BETWEEN '2012-05-02' 
   AND '2012-06-26' and pdate ORDER BY pdate ASC

2 个答案:

答案 0 :(得分:2)

创建子查询并按MAX排序......

SELECT * FROM (
SELECT pdate,MAX(Score) as maxscore FROM tblpulse 
WHERE DID = '171488' && pdate BETWEEN '2012-05-02' 
AND '2012-06-26' and pdate ORDER BY pdate ASC)
ORDER BY maxscore DESC LIMIT 1

答案 1 :(得分:0)

select pdate, score
  from tblpulse
 where ... and pdate between '2012-05-02' and '2012-06-26'
   and score = ( select max( score ) from tblpulse where pdate between '2012-05-02' and '2012-06-26')

(我省略了DID的事情,因为问题中没有描述该列。)