2012-05-22 4 views
0

Ma table MySQL est la suivante:requête MySQL - compter comme termes

ID term timestamp 

1 foo xxxx 
2 bar xxxx 
3 bar xxxx 
4 foo xxxx 
5 foo xxxx 
6 foo xxxx 
7 xxx xxxx 

Je souhaite produire une liste affichant les termes les plus courants pour la quantité de ce terme

-à-dire le résultat final:

foo 
bar 
xxx 

Quelle requête dois-je utiliser? J'ai fait un peu de googling, mais je ne suis pas sûr de ce que je cherche - COUNT/GROUP BY etc.

Répondre

4

La requête SQL suivante devrait fournir une solution à votre problème.

SELECT term 
FROM tablename 
GROUP BY term 
ORDER BY COUNT(*) DESC 
+0

Parfait. Je vous remercie! –

2
SELECT COUNT(*), term FROM table GROUP BY term ORDER BY COUNT(*) DESC; 
1

Essayez

SELECT term, Count(*) AS cnt FROM mytable GROUP BY term ORDER BY cnt DESC 
0
SELECT TERM 
FROM @table 
GROUP BY TERM 
HAVING COUNT(*) > 5 --replace with number you want 
ORDER BY COUNT(*) 
1

GROUP BY est correcte. Essayez ceci:

SELECT term FROM table 
GROUP BY term 
ORDER BY COUNT(term) desc 
0

Cette requête SQL devrait le faire:

SELECT term FROM my_table GROUP BY term ORDER BY COUNT(term) DESC;