0

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

+------------+-------+
| 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
4

2 回答 2

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
于 2012-06-26T22:41:18.513 回答
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 的事情,因为问题中没有描述该列。)

于 2012-06-26T22:43:11.527 回答