为了账号安全,请及时绑定邮箱和手机立即绑定

在MYSQL中联接具有SUM问题的表

在MYSQL中联接具有SUM问题的表

喵喔喔 2019-10-22 21:27:48
我一直在获取连接表上的SUM时总是遇到麻烦,总是有一个问题,我可以通过运行两个查询来获得所需的结果,我想知道这两个查询是否可以组合在一起以构成一个连接查询,这是我查询的结果有和我尝试加入查询查询1SELECT last_name, first_name, DATE_FORMAT( (mil_date),  '%m/%d/%y' ) AS dates, SUM( drive_time ) MINUTES FROM bhds_mileage LEFT JOIN bhds_teachers i ON i.ds_id = bhds_mileage.ds_id WHERE mil_date BETWEEN  '2016-04-11' AND  '2016-04-30'AND bhds_mileage.ds_id =5GROUP BY CONCAT( YEAR( mil_date ) ,  '/', WEEK( mil_date ) ) ,    bhds_mileage.ds_idORDER BY last_name ASC , dates ASC 以分钟为单位的输出是271、281、279查询2SELECT last_name, first_name, DATE_FORMAT((tm_date), '%m/%d/%y') AS dates,   SUM(tm_hours) total FROM bhds_timecard LEFT JOIN bhds_teachers i ON i.ds_id = bhds_timecard.ds_id WHERE tm_date BETWEEN '2016-04-11' AND '2016-04-30' AND bhds_timecard.ds_id = 5GROUP BY CONCAT(YEAR(tm_date), '/', WEEK(tm_date)), bhds_timecard.ds_id ORDER BY last_name ASC, dates ASC这里的输出是33.00,36.00,26.75现在我尝试加入查询SELECT last_name, first_name, DATE_FORMAT((tm_date), '%m/%d/%y') AS dates,  SUM(tm_hours) total,  SUM( drive_time ) MINUTES FROM bhds_timecard LEFT JOIN bhds_teachers i ON i.ds_id = bhds_timecard.ds_id LEFT JOIN bhds_mileage ON DATE_FORMAT((bhds_timecard.tm_date), '%m/%d/%y') = DATE_FORMAT((bhds_mileage.mil_date), '%m/%d/%y') AND bhds_timecard.ds_id = bhds_mileage.ds_idWHERE tm_date BETWEEN '2016-04-11' AND '2016-04-30' AND bhds_timecard.ds_id = 5GROUP BY CONCAT(YEAR(tm_date), '/', WEEK(tm_date)), bhds_timecard.ds_id 括号是预期的这输出1044(271),1086(281),1215(279)
查看完整描述

2 回答

  • 2 回答
  • 0 关注
  • 640 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信