草莓果冻⁣  2022-04-13 17:58 采纳率: 64.3%
浏览 70

关于mysql查询语句的问题!

共有两张汇总表
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;

img

以上查询语句中问号的地方填入t.cre_date却获取不到父级的select的t.cre_date
请问需要这么更改sql语句?

  • 写回答

1条回答 默认 最新

  • haikuokuangtu 2022-04-14 14:37
    关注

    嵌套太深了,超过2层就传递不进去了,你把BETWEEN '20220414' AND '20220416'放到?那里不也可以吗。

    评论

报告相同问题?

问题事件

  • 修改了问题 4月14日
  • 修改了问题 4月14日
  • 修改了问题 4月14日
  • 修改了问题 4月14日
  • 展开全部

悬赏问题

  • ¥15 两台交换机分别是trunk接口和access接口为何无法通信,通信过程是如何?
  • ¥15 C语言使用vscode编码错误
  • ¥15 用KSV5转成本时,如何不生成那笔中间凭证
  • ¥20 ensp怎么配置让PC1和PC2通讯上
  • ¥50 有没有适合匹配类似图中的运动规律的图像处理算法
  • ¥15 dnat基础问题,本机发出,别人返回的包,不能命中
  • ¥15 请各位帮我看看是哪里出了问题
  • ¥15 vs2019的js智能提示
  • ¥15 关于#开发语言#的问题:FDTD建模问题图中代码没有报错,但是模型却变透明了
  • ¥15 uniapp的h5项目写一个抽奖动画