Mysql计数id查询

时间:2010-10-17 16:45:45

标签: sql mysql datetime-generation

我正在使用像这样的mysql表:

|  userid  |  regdate   | question  | answer  |
-----------------------------------------------
      1      2010-10-14   question 1  answer1
      2      2010-10-14   question 2  answer2    
      3      2010-10-15   question 3  answer3
      4      2010-10-16   question 4  answer4

我想计算每天的注册用户数以及每天发送的问题和答案的数量。我可以用一个mysql查询来完成吗?

结果应该是这样的:

|  regdate  |  new users  |  questions sent  | answers sent  |
--------------------------------------------------------------
 2010-10-14        2               2                 2
 2010-10-15        1               1                 1
 2010-10-16        1               1                 1

2 个答案:

答案 0 :(得分:0)

也许:

SELECT regadte, 
       COUNT(userid) as new_users, 
       COUNT(question) as questions_sent, 
       COUNT(answer) as answers_sent 
FROM table 
GROUP BY regdate;

我不确定,因为,您的意思是“新注册用户”还是“登录用户”?此外,从您的表中,看起来用户必须为每个问题发送答案,我怀疑是这样的......

答案 1 :(得分:0)

试试这个

select regdate,count(userid) as new_users,count(question) as question_sent,count(answer) as answers_sent
from table_name group by regdate