2019-05-20 17:59:41 1616次浏览 1条回答 0 悬赏 50 金钱
SELECT sum(gross_margin) AS `gross_sum` FROM `yn_goods_analyze_shop_by_day` WHERE (`create_date` BETWEEN '2019-05-13' AND '2019-05-19') AND (spid in (SELECT `spid` FROM `yn_goods_analyze_shop_by_day` WHERE `create_date` BETWEEN '2019-05-13' AND '2019-05-19' GROUP BY `spid` HAVING round(sum(saleroom)*0.88-sum(goods_cost)-6,1) < 0))

表头统计的合计 执行时间太长 搜索总是超时,希望大神指点一下,这个 SQL 语句该怎么优化

补充于 2019-05-21 10:36

微信截图_20190521103444.png

微信截图_20190521103456.png
最后解决的方案

最佳答案

  • 花椒 发布于 2019-05-21 09:20 举报

    in 改为join模式

    2 条回复
    回复于 2019-05-21 09:51 回复

    SELECT sum(gross_margin) AS gross_sum FROM yn_goods_analyze_shop_by_day WHERE (create_date BETWEEN '2019-05-13' AND '2019-05-19') AND (spid join (SELECT spid FROM yn_goods_analyze_shop_by_day WHERE create_date BETWEEN '2019-05-13' AND '2019-05-19' GROUP BY spid HAVING round(sum(saleroom)*0.88-sum(goods_cost)-6,1) < 0))
    改成这样吗 ? 这样会报语法错误

    回复于 2019-05-21 09:53 回复


    SELECT sum(gross_margin) AS gross_sum FROM yn_goods_analyze_shop_by_day join (SELECT spid FROM yn_goods_analyze_shop_by_day WHERE create_date BETWEEN '2019-05-13' AND '2019-05-19' GROUP BY spid HAVING round(sum(saleroom)*0.88-sum(goods_cost)-6,1) < 0) AS u WHERE (create_date BETWEEN '2019-05-13' AND '2019-05-19')
    这样写得到的结果不对 应该怎么写大佬 求指导 QAQ

    没有找到数据。
您需要登录后才可以回答。登录 | 立即注册
EizoPeter
总监

EizoPeter

注册时间:2018-08-30
最后登录:2020-06-18
在线时长:34小时12分
  • 粉丝6
  • 金钱2775
  • 威望0
  • 积分3115

热门问题