【问题标题】:Laravel Query to Group by two different datesLaravel 查询按两个不同日期分组
【发布时间】:2016-12-01 22:15:37
【问题描述】:

假设我正在搜索输入变量: $从 :01-01-2016 $到 : 03-12-2016

这是我当前在表中的表:(outreach_links)

created_at   start_date     status 
---------------------------------
2016-12-01   2016-01-01    A
2016-12-02   2016-02-01    A
2016-12-03   2016-03-01    A
2016-12-03   2016-04-01    A
2016-12-03      NULL       P
2016-12-03      NULL       P
2016-12-03      NULL       P

我想在查询后这样输出>>

Month          P        A
---------------------------
Jan/2016       0        1
Feb/2016       0        1
March/2016     0        1
April/2016     0        1
Dec/2016       3        0

逻辑是我想通过 created_at 对 Status = "P" 进行分组,而我想通过 start_date 对 Status="A" 进行分组?如何在查询中完成此操作?

这是我当前的 laravel 5.2 查询:

DB::select(
    DB::raw('select 
        sum(case when outreach_links.status="P" and outreach_links.created_at >= ? and outreach_links.created_at <=? then 1 else 0 end) as pp,
        sum(case when outreach_links.status="A" and outreach_links.start_date >= ? and outreach_links.start_date <=? then 1 else 0 end) as aa,
        sum(case when outreach_links.status="A" then outreach_links.cost else 0 end) as cc,
        MONTH(outreach_links.created_at) month,
        MONTHNAME(outreach_links.created_at) month_name,
        YEAR(outreach_links.created_at) year from outreach
        inner join outreach_links on outreach.id = outreach_links.outreach_id
        where outreach.profile_id=?
        group by month, year'),
    [$from, $to, $from, $to, $pro_id]
);

因为我只按 (created_at) 分组,所以这个输出是错误的:

    Month          P        A 
    ---------------------------
    Dec/2016       3        4

这是我查询时的输出 >> var_dump();

array(1) {
  [0]=>
  object(stdClass)#369 (6) {
    ["pp"]=>
    string(1) "3"
    ["aa"]=>
    string(1) "4"
    ["cc"]=>
    string(4) "0.00"
    ["month"]=>
    string(2) "12"
    ["month_name"]=>
    string(8) "December"
    ["year"]=>
    string(4) "2016"
  }
}

你能帮我修改查询吗?

谢谢

【问题讨论】:

  • 还有其他帮助吗?

标签: php mysql laravel-5


【解决方案1】:

这样就可以了:

select concat(monthname(date), '/', year) as Month,
(select count(*) from test where year(created_at) = year and month(created_at) = month and status = 'P') as P,
(select count(*) from test where year(start_at) = year and month(start_at) = month and status = 'A') as A
from
(select year(created_at) as year, month(created_at) as month, created_at as date
from test where created_at >= ? and created_at <= ?
union
select year(start_at) as year, month(start_at) as month, start_at as date
from test where start_at >= ? and start_at <= ?) t1
group by year, month
order by date

基本上,它UNIONs 来自该表的所有日期,并在此之上执行group by。

这里是SQL Fiddle。

【讨论】:

  • 我有没有用这个查询内部连接另一个表??
  • $from 和 $to 输入的日期在哪里?
  • 他不能正常工作,因为 $from 和 $to 是变量,它们可以改变我如何添加它们以使我工作
  • 您可以在内部查询中添加 $from 和 $to 输入来限制日期。让我编辑答案。
  • @user3150060 我在内部查询中添加了日期。此外,您可以通过将此查询包含在 from 关闭中来将另一个表与此查询进行内部连接。看看这个:joyofdata.de/blog/…
猜你喜欢
  • 1970-01-01
  • 2019-04-03
  • 2013-08-18
  • 1970-01-01
  • 1970-01-01
  • 2013-11-05
  • 2020-07-08
  • 2021-02-22
  • 1970-01-01
相关资源
最近更新 更多