Php 需要MySQL查询的帮助吗

Php 需要MySQL查询的帮助吗,php,cakephp,mysql,Php,Cakephp,Mysql,我需要MySQL查询的帮助。以下是我数据库的一部分: +---------------+ | users | +---------------+ | id |--- | username | | +-----------------+ +---------------------+ | first_name | | |users_in_projects| | regular_posts | | la

我需要MySQL查询的帮助。以下是我数据库的一部分:

+---------------+
| users         |
+---------------+
| id            |---
| username      |  |    +-----------------+     +---------------------+
| first_name    |  |    |users_in_projects|     | regular_posts       |
| last_name     |  |    +-----------------+     +---------------------+
| email         |  |    | id              |--   | id                  |
| password      |  |    | project_id      | |   | date                |
| active        |  |----| user_id         | |   | size                |
| created       |       +-----------------+ |---| users_in_project_id |
| modified      |                               +---------------------+
| group_id      |
+---------------+
各协会:

User hasMany UsersInProject
UsersInProject belongsTo User
UsersInProject hasMany RegularPost
RegularPost belongsTo UsersInProject
我想要的是带有以下内容的结果表:

username
summary
每月定期发布的大小,例如2010年,如下所示:

username 2010-01 2010-02 2010-03 2010-04 2010-05 2010-06 2010-07 2010-08 2010-09 2010-10 2010-11 2010-12
foo        0.75    0.75    1        1      1       0.5    0.75    0.75     0.75    0.75    0.75   0.75
bar         0.5     0.5   0.5      0.5     1       0.75   0.75      1        1       1       1      1
...
或类似以下的数组:

Result => array (
        [0] => array (
                user.id => 1
                user.username => foo
                RegularPost => array (
                        [0] => array (
                                date => 2010-01
                                size => 0.75
                        )
                        [1] => array (
                                date => 2010-02
                                size => 0.75
                        )
                        ...
                )
        )
        [1] => array (
                user.id => 2
                user.username => bar
                RegularPost => array (
                        [0] => array (
                                date => 2010-01
                                size => 0.5
                        )
                        [1] => array (
                                date => 2010-02
                                size => 0.5
                        )
                        ...
                )
        )
        ...
)
我可以为一个用户编写查询,但每个用户都不能

$results = $this->User->UsersInProject->RegularPost->find('all', array(
            'recursive' => 0,
            'fields'=> array(
                        'RegularPost.id',
                        'RegularPost.date',
                        'SUM(RegularPost.size) AS size',
                ),
            'conditions' => array(
                'UsersInProject.user_id' => $id,
                'RegularPost.date like' => '2010%',
            ),
            'group' => array('RegularPost.date')
        ));

SELECT
        `RegularPost`.`date`,
        SUM(`RegularPost`.`size`) AS size
FROM
        `regular_posts` AS `RegularPost`
LEFT JOIN
        `users_in_projects` AS `UsersInProject`
ON
        (`RegularPost`.`users_in_project_id` = `UsersInProject`.`id`)
WHERE
        `UsersInProject`.`user_id` = 1 AND `RegularPost`.`date` like '2010%'
GROUP BY
        `RegularPost`.`date`;
结果是:

+------------+---------+
| date       | size    |
+------------+---------+
| 2010-01-01 | 0.75000 |
| 2010-02-01 | 0.75000 |
| 2010-03-01 | 0.75000 |
| 2010-04-01 | 0.75000 |
| 2010-05-01 | 0.75000 |
| 2010-06-01 | 1.00000 |
| 2010-07-01 | 0.75000 |
| 2010-08-01 | 0.75000 |
| 2010-09-01 | 1.00000 |
| 2010-10-01 | 0.75000 |
| 2010-11-01 | 0.75000 |
| 2010-12-01 | 1.00000 |
+------------+---------+

请帮忙。

你试过这个吗。。。是否从where子句中删除用户id并将其添加到group by子句中

SELECT
        `RegularPost`.`date`,
        SUM(`RegularPost`.`size`) AS size
FROM
        `regular_posts` AS `RegularPost`
LEFT JOIN
        `users_in_projects` AS `UsersInProject`
ON
        (`RegularPost`.`users_in_project_id` = `UsersInProject`.`id`)
WHERE
        `RegularPost`.`date` like '2010%'
GROUP BY
        `UsersInProject`.`user_id`, `RegularPost`.`date`;

如果您试图让它为所有用户显示,请从查询中删除“UsersInProject.user_id=1”部分。另外,要获取要在结果中显示的用户名,需要告诉查询从表中获取该信息。也许我错过了你想做的事?