如何在mysql中的一个查询中选择和选择计数?

时间:2014-10-13 16:47:46

标签: mysql select count

我有这张桌子:

Activity     Date    
reading   12-10-2014
watching  12-10-2014
reading   13-10-2014
reading   12-10-2014
watching  13-10-2014 

我想要做的是选择活动并根据日期计算活动编号,我希望输出将是这样的(条件:日期=' 12-10-2014'):

Activity   count    date
reading      2    12-10-2014
watching     1    12-10-2014

我该怎么办?请帮帮我。感谢。

2 个答案:

答案 0 :(得分:1)

SELECT Activity,COUNT(1) `count`,date
FROM mytable WHERE date='12-10-2014'
GROUP BY date,Activity;

SELECT Activity,COUNT(1) `count`,date
FROM mytable WHERE date='2014-10-12'
GROUP BY date,Activity;

确保您的表具有日期和活动

的索引
ALTER TABLE mytable ADD INDEX date_Activity_ndx (date,Activity);

答案 1 :(得分:0)

这应该有效:

select activity, count(activity) as count, date from my_table
where date = '12-10-2014' group by activity;

(不确定您的列标签 - 您可能需要调整大小写)