1

我想统计过去 4 个月每月有多少用户提交内容。我在考虑一个 MySQL UNION,但不认为它会起作用,因为它可能会显示重复的行,因为 AFAIKUNION只附加 2 个查询结果集。

我的表结构是这样的(node来自 Drupal 的典型表——对于 Drupal 大师):

+-----------+------------------+------+-----+---------+----------------+
| Field     | Type             | Null | Key | Default | Extra          |
+-----------+------------------+------+-----+---------+----------------+
| nid       | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| vid       | int(10) unsigned | NO   | UNI | 0       |                |
| type      | varchar(32)      | NO   | MUL |         |                |
| language  | varchar(12)      | NO   |     |         |                |
| title     | varchar(255)     | NO   | MUL |         |                |
| uid       | int(11)          | NO   | MUL | 0       |                |
| status    | int(11)          | NO   | MUL | 1       |                |
| created   | int(11)          | NO   | MUL | 0       |                |
| changed   | int(11)          | NO   | MUL | 0       |                |
| comment   | int(11)          | NO   |     | 0       |                |
| promote   | int(11)          | NO   | MUL | 0       |                |
| sticky    | int(11)          | NO   |     | 0       |                |
| tnid      | int(10) unsigned | NO   | MUL | 0       |                |
| translate | int(11)          | NO   | MUL | 0       |                |
+-----------+------------------+------+-----+---------+----------------+

保存节点提交时间的列是created

4

3 回答 3

2

也许是这样的(看起来不那么优雅......)

select count(distinct(t1.uid)) from node t1
inner join node t2 on t2.uid = t1.uid and t2.created between date_add(now(), interval -2 month) and date_add(now(), interval -1 month)
inner join node t3 on t3.uid = t1.uid and t3.created between date_add(now(), interval -3 month) and date_add(now(), interval -2 month)
inner join node t4 on t4.uid = t1.uid and t4.created between date_add(now(), interval -4 month) and date_add(now(), interval -3 month)
where t1.created between date_add(now(), interval -1 month) and now()
于 2012-05-02T11:12:35.423 回答
1

像这样的东西?

select extract(MONTH from create), count(distinct uid)
from node
where create >= DATE_ADD(month,-4,CURDATE())
group by extract(MONTH from create)

我没有测试过...

于 2012-05-02T11:32:53.620 回答
0
SELECT MONTH(create) as Month,
        (SELECT COUNT(vid) FROM Table WHERE MONTH(create) = Month)
FROM Table
ORDER BY Month DECS
LIMIT 4
于 2012-05-02T12:07:13.023 回答