Doctrine,Symfony3使用间隔从表格中选择日期范围

时间:2016-09-06 15:41:58

标签: mysql symfony doctrine-orm

让一切都清楚我有这两个表

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

用户必须选择两个日期开始和结束 让我说他\她选择了开始= 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命令

提前致谢

1 个答案:

答案 0 :(得分: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);