Codeigniter 3按表格的几个月组织/获取数据

时间:2016-09-23 13:35:46

标签: php mysql codeigniter

我有一个表名'比赛'

列的结构是这样的

id|gameTitle|username|username_p1|is_random|status|entry_fee|prize_money|created_at|updated_at

我想要的是获取过去6个月的数据。用户在一个月内玩过一些随机游戏的地方。

where is_random = 1
and username= 'anyUsername' or username_p1 = 'anyUsername'

从下面查询我只得到一个月的结果。

SELECT gameTitle, id, COUNT(created_at)
FROM  `competitions_1_1` 
WHERE is_random =1
AND username =  'guppuboss'
OR username_p1 =  'guppuboss'
AND created_at
BETWEEN  '2016-08-25 18:02:51'
AND  '2016-09-26 17:09:57'
GROUP BY created_at

我想要实现的是这样的事情:

2016-09-26  | 20
2016-08-26  | 9
2016-07-26  | 33

2 个答案:

答案 0 :(得分:0)

SELECT gameTitle, id, month(created_at) as month, year(created_at) as year, COUNT(*) as count
FROM  `competitions_1_1` 
WHERE is_random =1
AND ( username =  'guppuboss'
OR username_p1 =  'guppuboss' )
AND created_at > DATE_SUB(now(), INTERVAL 6 MONTH)   GROUP BY MONTH(created_at)

答案 1 :(得分:0)

您需要使用圆括号添加分组....

SELECT gameTitle, COUNT(id) as total
FROM  `competitions_1_1` 
WHERE (is_random = 1
AND username =  'any_name')
OR (is_random = 1 AND username_p1 =  'any_name')
AND created_at >= now()-interval 6 month

我希望你能找到你的解决方案..我知道还有任何问题