2016-09-06 39 views
0

,使一切清楚我有這兩個表學說,採用間隔

table 1 
------------------------------- 
id | name | email | created_at 
x | x | x | 2016:09:01 
x | x | x | 2016:09:01 
x | x | x | 2016:09:01 
x | x | x | 2016:09:02 
x | x | x | 2016:09:04 
x | x | x | 2016:09:04 
------------------------------- 

table 2 
------------------------------- 
id | name | email | created_at 
x | x | x | 2016:09:03 
x | x | x | 2016:09:03 
x | x | x | 2016:09:05 
------------------------------- 

從表日期Symfony3選擇範圍,用戶將不得不選擇兩個日期開始和結束 讓說他\她選擇了開始= 2016:09:01和結束= 2016:09:07 所以我需要一個結果像這樣顯示

result 
------------------------------- 
num_of_recs_t1 | num_of_recs_t2 | day 
3    | 0    | 2016:09:01 
1    | 0    | 2016:09:02 
0    | 2    | 2016:09:03 
2    | 0    | 2016:09:04 
0    | 1    | 2016:09:05 
0    | 0    | 2016:09:06 
0    | 0    | 2016:09:07 
在控制器

我使用Symfony的3教義,老實說我甚至不認爲我可以用正常的mysql使命令

在此先感謝

+0

你跟主義合作ORM?或者只是DBAL? –

+0

@dragoste ORM –

+0

如果你使用ORM,那麼你應該考慮對象而不是表格。學說將會更容易理解這種方式。 –

回答

0

試試這個

$db = $this->getDoctrine()->getConnection(); 

$data = $db->fetchAll(" 
select 
    t1.num_of_recs as num_of_recs_t1, 
    t2.num_of_recs as num_of_recs_t2, 
    t1.created_at 
from 
    (
    select created_at, count(*) as num_of_recs 
    from table_1 
    group by created_at 
) t1 
    join (
    select created_at, count(*) as num_of_recs 
    from table_2 
    group by created_at 
) t2 
    on t1.created_at = t2.created_at 
order by 
    t1.created_at"); 

dump($data); 
+0

但這一個不會得到丟失的日期,如06-09和07-09 –

+0

用空填充零數組用PHP填充數組,我認爲這是不可能的,只能通過SQL –