共有两张汇总表
t_summary_pos_benefit_everyday
t_summary_mpos_benefit_everyday
两个表的字段:cre_date,performance,user_id
想要查询统计某个时间段的两张表的总performance以及上个月这个时间段的总performance
大致需要查询出的结果:
| cre_date | user_id | performance | performance_lastmonth |
| 20220414 | 76 | 640 | 940 |
| 20220415 | 76 | 650 | 950 |
| 20220416 | 76 | 660 | 960 |
SELECT
t.cre_date,sum( t.performance )AS performance ,
(SELECT sum( t1.performance ) FROM (SELECT tt.cre_date,SUM( tt.performance ) AS performance
FROM t_summary_pos_benefit_everyday tt
WHERE tt.user_id = '71' AND tt.cre_date = date_format( subdate( ?, INTERVAL 1 MONTH ), '%Y%m%d' )
GROUP BY tt.cre_date
UNION ALL
SELECT tt.cre_date,SUM( tt.performance ) AS performance
FROM t_summary_mpos_benefit_everyday tt
WHERE tt.user_id = '71' AND tt.cre_date = date_format( subdate( ?, INTERVAL 1 MONTH ), '%Y%m%d' )
GROUP BY tt.cre_date) t1
GROUP BY t1.cre_date
) AS performance_lastmonth
FROM
(SELECT cre_date,SUM(performance) AS performance
FROM t_summary_pos_benefit_everyday
WHERE user_id = '71' AND cre_date BETWEEN '20220414' AND '20220416'
GROUP BY cre_date
UNION ALL
SELECT cre_date,SUM( performance ) AS performance
FROM t_summary_mpos_benefit_everyday
WHERE user_id = '71' AND cre_date BETWEEN '20220414' AND '20220416'
GROUP BY cre_date ) t
GROUP BY t.cre_date;
以上查询语句中问号的地方填入t.cre_date却获取不到父级的select的t.cre_date
请问需要这么更改sql语句?