简体   繁体   English

需要有关MySQL查询的帮助

[英]Need help with MySQL query

I need help with MySQL query. 我需要有关MySQL查询的帮助。 Here is a part of my database: 这是我的数据库的一部分:

+---------------+
| 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      |
+---------------+

and the associations: 和协会:

User hasMany UsersInProject
UsersInProject belongsTo User
UsersInProject hasMany RegularPost
RegularPost belongsTo UsersInProject

What I want is the result table with: 我想要的是结果表:

username and summary size of regular posts in every month for example year 2010, something like this: 每个月(例如2010年)的常规帖子的usernamesummary大小,例如:

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
...

or array like: 或类似的数组:

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
                        )
                        ...
                )
        )
        ...
)

I can write the query for one user, but for every don't. 我可以为一个用户编写查询,但对于每个用户都不可以。

$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`;

And the result: 结果:

+------------+---------+
| 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 |
+------------+---------+

Please help. 请帮忙。

Have you tried this... removing the user id from the where clause and adding it into the group by clause? 您是否尝试过...从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`;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM