如何选择一个表的相同列两次作为两个不同的列分开日期

时间:2018-04-18 09:28:06

标签: mysql

我有以下mysql查询,但执行大约需要5分钟,我怎样才能减少执行时间

    select
    revenue_center_group.description as rc_group,
        revenue_center.description as revenue_center,
        count(ytd.members) as total_ytd_members,
        count(lytd.members) as total_lytd_members
    from revenue_center
    left join revenue_center_group on revenue_center_group.id = revenue_center.revenue_center_group_id
    left join (
        select membership.id as members,
            membership.rc_id as rc_id
    from membership
    where membership.status =1 
    and DATE(membership.join_date)>= (SELECT DATE_FORMAT((SELECT DATE_FORMAT(date(now()) - INTERVAL 1 MONTH, '%Y-%m-%d')) ,'%Y-01-01 00:00:00'))
    and DATE(membership.join_date)<= (SELECT DATE_FORMAT(LAST_DAY(date(now()) - INTERVAL 1 MONTH), '%Y-%m-%d 23:59:59'))ytd on revenue_center.id = ytd.rc_id 
    left join (
        select
            membership.id as members,
            membership.rc_id as rc_id
    from membership
    where membership.status =1 
    and DATE(membership.join_date)>= (SELECT DATE_FORMAT((SELECT DATE_FORMAT((SELECT DATE_FORMAT(date(now()) - INTERVAL 1 MONTH, '%Y-%m-%d'))
  - INTERVAL 1 YEAR, '%Y-%m-%d')) ,'%Y-01-01 00:00:00'))
and DATE(membership.join_date)<= (SELECT DATE_FORMAT((select DATE_FORMAT(LAST_DAY(date(now()) - INTERVAL 1 MONTH), '%Y-%m-%d')
- INTERVAL 1 YEAR),'%Y-%m-%d 23:59:59'))lytd on revenue_center.id = lytd.rc_id 
    group by revenue_center_group.description,revenue_center.description with rollup;

0 个答案:

没有答案