MYSQL语句没有向我显示正确的vaules

时间:2016-12-06 05:40:32

标签: php mysql

我是MySQL / PHP领域的新手,我自己学习。我正在尝试提供已发货项目的数据,这是我正在使用的查询: -

import java.util.*; // import java course for Scanner class
public class Integers {


    public static void main(String[] args) {
        List<Integer> intList = new ArrayList<Integer>();


        Scanner console = new Scanner(System.in);
        System.out.println("Please input a series of numbers");
        while (console.hasNext())
        {

            String str = console.next();
            try
            {
                if(str.equals("quit")){
                    break;
                }
                int inputs = Integer.parseInt(str);

                System.out.println("the integer values are" +inputs);
                intList.add(inputs);
            }
            catch (java.util.InputMismatchException|NumberFormatException e)
            {
                console.nextLine();
            }


        }
        console.close();
        double d = Oddvalues(intList);
        System.out.println("the percent is" +d);
    }

    public static double Oddvalues (List<Integer> list) {
        int count = 0;
        for( Integer i : list)
        {

            if(!(i%2==0))
            {
                count++;
            }
        }

        double percentage = (Double.valueOf(String.valueOf(count))/ Double.valueOf(String.valueOf(list.size())))*100;

        return percentage; 

    }
}

我收到了正确的select jobs.job_number, jobs.employee_end, jobs.qty_comp, jobs.`date`,SUM(jobs.scrap), SUM(jobs.job_time) FROM jobs where jobs.job_number in ( SELECT j.job_number FROM jobs j WHERE j.moved_to = 'shipped' AND j.`date` BETWEEN '2016-11-28' AND '2016-12-02' AND j.qty_comp > 0 ) GROUP BY jobs.job_number SUM(scrap)以及SUM(job_time),但未向我显示job_numbers的最后一个条目。它显示moved_to = 'shipped', qty_comp > 0的第一次扫描的第一个条目。

感谢您提供任何帮助和建议。

1 个答案:

答案 0 :(得分:0)

可以将查询重写为(不需要子查询):

SELECT jobs.job_number, jobs.employee_end, jobs.qty_comp, jobs.`date`,
SUM(jobs.scrap), SUM(jobs.job_time)
FROM jobs AS j
WHERE j.moved_to = 'shipped' AND 
j.`date` BETWEEN '2016-11-28' AND '2016-12-02' AND
j.qty_comp > 0
GROUP BY jobs.job_number

“IN”条件不是必需的,因为聚合(分组依据)是job_number只有jobs.job_number,SUM(jobs.scrap),SUM(jobs.job_time)将有有意义的值。

由于jobs jobs.employee_end,jobs.qty_comp,jobs。date列来自多行,因此聚合只能提供1个值,在这种情况下是第一个值。通过在这些列上添加MAX聚合可能获得的最后一个值。