MySQL:多个连接及其对总和的影响

时间:2016-06-15 19:03:20

标签: mysql join sum

我正在尝试连接多个表并使用SUM命令,当我加入多个表时,我无法使SUM正常工作。从我在这个主题上所做的(这里和其他地方)的阅读中,我相当确定解决方案是在我加入之前求和,但我很难完成这项工作。

如果我运行以下代码,只加入两个表,它可以完美地运行:

$scope.today = function() {
                                $scope.dt = new Date();
                            };
                            $scope.today();

                        $scope.clear = function() {
                            $scope.dt = null;
                        };

                        $scope.inlineOptions = {
                            customClass : getDayClass,
                            minDate : new Date(),
                            showWeeks : true
                        };

                        $scope.dateOptions = {

                            formatYear : 'yy',
                            maxDate : new Date(2199, 12, 31),
                            minDate : new Date(),
                            startingDay : 1
                        };

                        $scope.toggleMin = function() {
                            $scope.inlineOptions.minDate = $scope.inlineOptions.minDate ? null
                                    : new Date();
                            $scope.dateOptions.minDate = $scope.inlineOptions.minDate;
                        };

                        $scope.toggleMin();

                        $scope.open1 = function() {
                            $scope.popup1.opened = true;
                        };

                        /*
                         * $scope.open2 = function() { $scope.popup2.opened =
                         * true; };
                         */

                        $scope.setDate = function(year, month, day) {
                            $scope.dt = new Date(year, month, day);
                        };

                        $scope.formats = [ 'dd-MMMM-yyyy', 'yyyy/MM/dd',
                                'dd.MM.yyyy', 'shortDate' ];
                        $scope.format = $scope.formats[0];
                        $scope.altInputFormats = [ 'M!/d!/yyyy' ];

                        $scope.popup1 = {
                            opened : false
                        };

                         $scope.dateOptions = {
                                    dateDisabled: disabled,
                                    formatYear: 'yy',
                                    maxDate: new Date(2020, 5, 22),
                                    minDate: new Date(),
                                    startingDay: 1
                                  };

                         // Disable weekend selection
                          function disabled(data) {
                            var date = data.date,
                              mode = data.mode;
                            return mode === 'day' && (date.getDay() === 0 || date.getDay() === 6);
                          }


                        /*
                         * $scope.popup2 = { opened : false };
                         */
                        var tomorrow = new Date();
                        tomorrow.setDate(tomorrow.getDate() + 1);
                        var afterTomorrow = new Date();
                        afterTomorrow.setDate(tomorrow.getDate() + 1);
                        $scope.events = [ {
                            date : tomorrow,
                            status : 'full'
                        }, {
                            date : afterTomorrow,
                            status : 'partially'
                        } ];

                        function getDayClass(data) {
                            var date = data.date, mode = data.mode;
                            if (mode === 'day') {
                                var dayToCheck = new Date(date).setHours(0,
                                        0, 0, 0);

                                for (var i = 0; i < $scope.events.length; i++) {
                                    var currentDay = new Date(
                                            $scope.events[i].date)
                                            .setHours(0, 0, 0, 0);

                                    if (dayToCheck === currentDay) {
                                        return $scope.events[i].status;
                                    }
                                }
                            }

                            return '';
                        }

如果我这样做,在连接中添加第三个表,它会破坏总和:

SELECT CONCAT(m.nameFirst, " ",m.nameLast) AS Name, sum(b.g) AS G,sum(b.ab) AS AB, sum(b.h) AS H, sum(b.hr) AS HR, sum(b.sb) AS SB, sum(b.bb) AS BB, ROUND(sum(b.h)/sum(b.ab), 3) AS BA, ROUND((sum(b.h)+sum(b.bb)+sum(b.hbp))/(sum(b.ab)+sum(b.bb)+sum(b.hbp)+sum(b.sf)), 3) AS OBP, ROUND((sum(b.h)+sum(b.2b)+2*sum(b.3b)+3*sum(b.hr))/sum(b.ab), 3) as SLG
FROM Master m
JOIN Batting b on m.playerID = b.playerID
WHERE birthMonth = 6 and birthDay = 15
Group by b.playerID
ORDER by b.h DESC

正如我所说,我很确定解决方案是在加入之前求和。我该怎么做呢?

提前致谢。

1 个答案:

答案 0 :(得分:0)

一种技术是汇总表格,然后在这些表格上加入。

SELECT CONCAT(m.nameFirst, " ",m.nameLast) AS Name
    , b.G AS G, b.AB AS AB
    , p.W as WINS
FROM Master m
LEFT JOIN 
  (SELECT playerID, SUM(g) AS G, SUM(ab) AS AB
   FROM Batting
   GROUP BY playerID) AS b
    ON m.playerID = b.playerID
LEFT JOIN 
   (SELECT playerID, sum(W) AS WINS
    FROM Pitching
    GROUP BY playerID) AS p 
  ON m.playerID = p.playerID
WHERE birthMonth = 6 and birthDay = 15

您总是可以将子查询移动到SELECT结构

SELECT CONCAT(m.nameFirst, " ",m.nameLast) AS Name
    , (SELECT SUM(g)
       FROM Batting AS b
       WHERE m.playerID = b.playerID) AS G
    , (SELECT SUM(ab)
       FROM Batting
       WHERE m.playerID = b.playerID) AS AB
    , (SELECT sum(W)
       FROM Pitching AS p
       WHERE m.playerID = p.playerID) AS WINS
FROM Master m
WHERE birthMonth = 6 and birthDay = 15