开发者

MYSQL - multiple count statments

I'm trying to do a lookup on our demographioc table to display some stats. However, since out demographic table is quit big I want to do it in one query.

There are 2 fields that are important: sex, last_login

I want to be able to get the total number of logins for various date ranges (<1day ago, 1-7 days ago, 7-30 days ago, etc) GROUPED BY sex

I right now know how to do it for one date range. For example less than 1 day ago:

SELECT sex, count(*) peeps 
  FROM player_account_demo 
 WHERE last_demo_update > 1275868800 
GROUP BY sex

Which returns:

sex       peeps
----------------
UNKNOWN   22
MALE      43
FEMALE    86

However I'd have to do this on开发者_如何学Cce for each range. Is there a way to get all 3 ranges in there?

I'd want my end result to look something like this:

sex       peeps<1day      peeps1-7days       peeps7-30days

Thanks!

IMPORTANT NOTE: last demo_update is the epoch time (unix time stamp)


SELECT sex,
  SUM(IF(DATEDIFF(NOW(),last_login) < 1,1,0)),
  SUM(IF(DATEDIFF(NOW(),last_login) BETWEEN 1 AND 7,1,0)),
  SUM(IF(DATEDIFF(NOW(),last_login) BETWEEN 7 AND 30,1,0))
FROM player_account_demo 
GROUP BY sex


You want to use a Pivot Table.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜