2012-06-26 164 views
0

我有一個包含日期和值的表。 我想要做的是獲取MAX()值的值和對應於該值的日期。 即Mysql,選擇最大值和匹配值

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

在相關欄中有很多類似的問題,像這樣:http://stackoverflow.com/questions/2584481/select-get-整行對應於max-in-mysql-group?rq = 1 – zerkms

+0

查詢中的DID列是什麼? – Marvo

回答

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 
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的事情,因爲該專欄沒有在問題中描述)。