MYSQL减去两个SELECT查询

时间:2020-01-18 23:56:41

标签: mysql

使用MYSQL,我编写了两个由UNION组合的大型SELECT查询,以获取2行,其中第一行是当前月份的计数,第二行是上个月的计数。查询如下:

select * from 
(select count(*) as type1 from table_x where nationality_id = 23 and month(START_DATE) = month(now())) as t1,
(select count(*) as type2 from table_x where nationality_id = 24 and month(START_DATE) = month(now())) as t2,
(select count(*) as type3 from table_x where nationality_id = 25 and month(START_DATE) = month(now())) as t3,
(select count(*) as type4 from table_x where nationality_id = 26 and month(START_DATE) = month(now())) as t4
UNION
select * from 
(select count(*) as type1 from table_x where nationality_id = 23 and month(START_DATE) = month(now() - INTERVAL 1 MONTH)) as t1,
(select count(*) as type2 from table_x  where nationality_id = 24 and month(START_DATE) = month(now() - INTERVAL 1 MONTH)) as t2,
(select count(*) as type3 from table_x where nationality_id = 25 and month(START_DATE) = month(now() - INTERVAL 1 MONTH)) as t3,
(select count(*) as type4 from table_x where nationality_id = 26 and month(START_DATE) = month(now() - INTERVAL 1 MONTH)) as t4

我想添加第三行,这是第二行和第一行之间的区别。 如何使用当前查询执行此操作?

1 个答案:

答案 0 :(得分:0)

您显然正在对当前月份和上个月进行比较。因此,我将从内部的预查询汇总开始,仅获取> =上个月第一天的交易以及您要查找的国籍ID中的记录。

DAYOFMONTH()-1天的内部date_sub()为您提供当前月份的第一个月。如果再减去一个月,则表示您是LAST月份的第一个月。

现在,您可以汇总每个国籍的总和与当前月份的总和。该内部查询为您提供所有开始和结束计数。现在包裹在外面,除了差异之外,您还可以获得所有计数...显然,您可以分别更改列名称。

select
        PQ.*,
        PQ.ThisMonth23 - PQ.LastMonth23 = Diff23,
        PQ.ThisMonth24 - PQ.LastMonth24 = Diff24,
        PQ.ThisMonth25 - PQ.LastMonth25 = Diff25,
        PQ.ThisMonth26 - PQ.LastMonth26 = Diff26
   from
      ( select 
                sum( case when t.Nationality_id = 23 and month( t.StartDate ) = month( now()) then 1 else 0 end ) ThisMonth23,
                sum( case when t.Nationality_id = 24 and month( t.StartDate ) = month( now()) then 1 else 0 end ) ThisMonth24,
                sum( case when t.Nationality_id = 25 and month( t.StartDate ) = month( now()) then 1 else 0 end ) ThisMonth25,
                sum( case when t.Nationality_id = 26 and month( t.StartDate ) = month( now()) then 1 else 0 end ) ThisMonth26,
                sum( case when t.Nationality_id = 23 and month( t.StartDate ) != month( now()) then 1 else 0 end ) LastMonth23,
                sum( case when t.Nationality_id = 24 and month( t.StartDate ) != month( now()) then 1 else 0 end ) LastMonth24,
                sum( case when t.Nationality_id = 25 and month( t.StartDate ) != month( now()) then 1 else 0 end ) LastMonth25,
                sum( case when t.Nationality_id = 26 and month( t.StartDate ) != month( now()) then 1 else 0 end ) LastMonth26
            from 
                table_x t
            where
                   t.StartDate >= date_sub( date_sub( t.StartDate, interval DAYOFMONTH( t.StartDate ) -1 DAY ), interval 1 MONTH )
               AND t.Nationality_id IN ( 23, 24, 25, 26 )
        ) PQ

我只想补充一下,您的查询可能比您想像的要多...您要查询所有记录,例如:今年1月REGARDLESS,而本年度所有记录则是12月REGARDLESS,因为您符合的所有条件都是基于MONTH()而没有考虑YEAR()。我明确地只查询当月和上个月。