2012-10-09 228 views
0

我非常的最大值忘了怎麼用SQL做到這一點其實,事情是我有一個SQL SELECT語句如:返回select語句SQL

SELECT COUNT(*) NAME FROM `SomeTable` WHERE `SomeID` = xxx GROUP BY `Field` 

這確實返回一個表有一個字段和許多包含數字的記錄。我想要的是獲得這些記錄中最大數量的單個值。

+1

如果我谷歌搜索你的問題題,答案第一和第二齣現。換句話說,你根本就不打算去研究。 – dan1111

+0

那麼[mysql MAX()](http://www.tutorialspoint.com/mysql/mysql-max-function.htm)函數呢? –

回答

1

只需添加LIMIT:

SELECT COUNT(`NAME`) AS `NUM` FROM `SomeTable` WHERE `SomeID` = xxx GROUP BY `Field` ORDER BY `NUM` DESC LIMIT 1 
+0

你不應該使用降序嗎? –

+0

是...添加了那個 –

+0

是不是應該在分組之前指定順序? –

1
select max(name) from 
(
SELECT COUNT(*) NAME FROM `SomeTable` WHERE `SomeID` = xxx GROUP BY `Field`)a 
1
SELECT COUNT(*) NAME 
FROM `SomeTable` 
WHERE `SomeID` = xxx 
GROUP BY `Field` 
order by NAME desc 
limit 1 
0

我想是讓那些 記錄中的最大數目的單個值。另外

SELECT MAX(MyField) NAME 
FROM `SomeTable` 
WHERE `SomeID` = xxx; 

,如果你想要一個總分組:

試試這個:

SELECT t1.* 
FROM SomeTable t1 
(
    SELECT SomeID, Max(Field) MaxField 
    FROM SomeTable 
    GROUP BY SomeID 
) t2 ON t1.SomeID = t2.SomeID AND t1.Field = t2.MaxField 
WHERE t1.SomeID = xxx 
0

如果你只是想對過濾器的最高值的一個結果,只是用馬克斯通過另一列,語法如下:

SELECT Name, MAX(MyField) as MaxOfMyField -- Or COUNT(MyField) if you want the Count 
FROM `SomeTable` 
WHERE `SomeID` = xxx 
GROUP By Name; 
0

爲MAX函數的語法是:

SELECT MAX(expression) 
FROM tables 
WHERE predicates; 

SELECT MAX(salary) as "Highest salary" 
FROM employees; 
2

它應該幫助你。只需使用MAX()功能

SELECT MAX(COUNT(*)) FROM `SomeTable` WHERE `SomeID` = xxx GROUP BY `Field`