MySQL - 加法,减法和计算天数

时间:2015-08-03 11:12:08

标签: php mysql

我想计算平均销售产品数量。表:

pieces | date                | status
-------------------------------------------
1      | 2015-07-27 12:00:01 | sold
2      | 2015-07-27 16:30:43 | sold
1      | 2015-07-28 13:17:11 | sold
1      | 2015-07-29 10:46:00 | returned

我必须添加所有“已售出”状态(4),减去所有“返回”(1)并除以(2015-07-29 - 2015-07-27)3。

我可以使用以下一个查询进行此操作:

SELECT SUM(pieces WHERE 'sold' - pieces WHERE 'returned' / date(max - min)) WHERE (date >= date_sub(NOW(), INTERVAL 30 days))

1 个答案:

答案 0 :(得分:0)

是。一种简单的方法是使用条件聚合:

root.setOnMouseMoved(new EventHandler<MouseEvent>() {
        @Override
        public void handle(MouseEvent event) {
            if ((event.getY() <= 35) && (!fMenuVisible)) {
                TranslateTransition clipPane_Slide = new TranslateTransition(Duration.millis(300), clipPane);
                clipPane_Slide.setFromY(0);
                clipPane_Slide.setToY(35);
                clipPane_Slide.setCycleCount(1);
                clipPane_Slide.setAutoReverse(false);

                TranslateTransition menuBar_Slide = new TranslateTransition(Duration.millis(300), menuBar);
                menuBar_Slide.setFromY(0);
                menuBar_Slide.setToY(35);
                menuBar_Slide.setCycleCount(1);
                menuBar_Slide.setAutoReverse(false);

                ParallelTransition ShowMenu = new ParallelTransition(); 
                ShowMenu.getChildren().addAll(clipPane_Slide, menuBar_Slide);
                ShowMenu.setCycleCount(1);
                ShowMenu.playFromStart();

                fMenuVisible = true;
            } else if ((event.getY() > 35) && (fMenuVisible)) {
                TranslateTransition clipPane_Slide = new TranslateTransition(Duration.millis(300), clipPane);
                clipPane_Slide.setFromY(35);
                clipPane_Slide.setToY(0);
                clipPane_Slide.setCycleCount(1);
                clipPane_Slide.setAutoReverse(false);

                TranslateTransition menuBar_Slide = new TranslateTransition(Duration.millis(300), menuBar);
                menuBar_Slide.setFromY(35);
                menuBar_Slide.setToY(0);
                menuBar_Slide.setCycleCount(1);
                menuBar_Slide.setAutoReverse(false);

                ParallelTransition HideMenu = new ParallelTransition(); 
                HideMenu.getChildren().addAll(clipPane_Slide, menuBar_Slide);
                HideMenu.setCycleCount(1);
                HideMenu.playFromStart();

                fMenuVisible = false;
            }
        }
    });