hao234ksdf 2015-06-01 09:14 采纳率: 100%
浏览 4883
已采纳

求助mysql查询语句,实现三张表数据统计

temp_instore 、 temp_outstore 、 temp_stock三张表,以temp_stock表数据为主,统计与mate_id、sd_price两个字段值在另外两张表中相等的记录,主要查询出mate_id、sd_price、(sd_quantity + os_quantity - is_quantity) as quantity
就是sd_price与os_price、is_price比较,三者相等按上述进行加减运算,若os_price或is_price与sd_price不等按零处理。可能描述的不是很清楚,举个例子吧:
mate_id=1 and sd_price= 10这条记录,与另外两张表统计时。在temp_outstore表中有条记录mate_id= 1但os_price不等于sd_price,所以按0处理。在tem_instore表中有条记录mate_id=1且is_price= sd_price,所以有值参与运算。最终计算结果是20+0-3=17

这该怎么设计查询语句啊,是连接查询还是怎么弄。不是太会,逻辑这块
以下插入三张表的截图:is_打头的字段是temp_instore表;os_打头的字段是temp_outstore表;sd_打头的字段是temp_stock表。
图片说明
图片说明
图片说明

  • 写回答

1条回答 默认 最新

  • danielinbiti 2015-06-01 09:34
    关注
    类似这样,没有调测,自己试试,针对是一对一,
     select mate_id,sd_price,sd_quantity+IFNULL(os_quantity,0)-IFNULL(is_quantity,0) quantity from temp_stock tsd left join temp_instore tin on tsd.mate_id=tin.mate_id and tsd.sd_price= tin.is_price
    left join temp_outstore tos on tsd.mate_id=tos.mate_id and tsd.sd_price= tos.is_price
    
    如果temp_instore 、 temp_outstore 、 temp_stock是一对多,
    select mate_id,sd_price, sum(quantity) quantity from(
    select mate_id,sd_price,sd_quantity+IFNULL(os_quantity,0)-IFNULL(is_quantity,0) quantity from temp_stock tsd left join temp_instore tin on tsd.mate_id=tin.mate_id and tsd.sd_price= tin.is_price
    left join temp_outstore tos on tsd.mate_id=tos.mate_id and tsd.sd_price= tos.is_price
    ) group by mate_id,sd_price
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮