我有一个名为trends_points的表,该表有以下列:
f 210
现在,我正在尝试对这个表运行一个查询,该查询将根据特定的term列在表中出现的次数(例如,如果表中有以下行)来获得特定时间框架中的行:
id | userId | term | time
------------------------------------
1 28 new year 1262231638
2 37 new year 1262231658
3 1 christmas 1262231666
4 34 new year 1262231665
5 12 christmas 1262231667
6 52 twitter 1262231669我希望排成这样的排出来:
new year
christmas
twitter这是因为“新年”在时间框架内存在三次,“圣诞节”存在两次,"twitter“只有一排。
到目前为止,我已经推断这是一个简单的地方,对于查询的特定时间框架部分和一个组来阻止相同的词在列表中出现两次。
这就产生了以下查询:
SELECT *
FROM `trends_points`
WHERE ( time >= <time-period_start>
AND time <= <time-period_end> )
GROUP BY `term`有人知道我会如何完成查询的最后部分吗?(将查询的结果按包含相同“术语”列值.的行数排序)。
发布于 2009-12-31 03:32:35
使用:
SELECT tp.term,
COUNT(*) 'term_count'
FROM TREND_POINTS tp
WHERE tp.time BETWEEN <time-period_start> AND <time-period_end>
GROUP BY tp.term
ORDER BY term_count DESC, tp.term见this question about why to use BETWEEN vs using the >=/<= operators。
请记住,在这种情况下,可以有联系--按默认顺序按术语值按字母顺序做空,但也可能有其他标准。
另外,如果您想进一步限制返回的行/术语的数量,可以将LIMIT clause添加到查询的末尾。例如,此查询将返回前五项:
SELECT tp.term,
COUNT(*) 'term_count'
FROM TREND_POINTS tp
WHERE tp.time BETWEEN <time-period_start> AND <time-period_end>
GROUP BY tp.term
ORDER BY term_count DESC, tp.term
LIMIT 5发布于 2009-12-31 03:11:12
快速回答:
SELECT
term, count(*) as thecount
FROM
mytable
WHERE
(...)
GROUP BY
term
ORDER BY
thecount DESC发布于 2009-12-31 03:13:39
SELECT t.term
FROM trend_points t
WHERE t.time >= <time-period_start> AND t.time <= <time-period_end>
ORDER BY COUNT(t.term) DESC
GROUP BY t.termhttps://stackoverflow.com/questions/1983745
复制相似问题