2010-08-18 235 views
0

我需要MySQL查詢幫助。這裏是我的數據庫的一部分:需要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 

我要的是與結果表:

usernamesummary尺寸普通帖子在每個月例如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 | 
+------------+---------+ 

請幫助。

+0

如果您試圖讓它顯示給所有用戶,請從查詢中取出「UsersInProject.user_id = 1」部分。另外,爲了讓用戶名顯示在結果中,您需要告訴查詢從表中獲取該信息。 也許我想念你想做的事情? – gabe3886 2010-08-18 11:13:02

回答

0

您是否嘗試過......從where子句中刪除用戶標識並將其添加到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`;