Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaFX MySQL查询仅显示customerId的第一个结果,但count显示两个客户_Mysql - Fatal编程技术网

JavaFX MySQL查询仅显示customerId的第一个结果,但count显示两个客户

JavaFX MySQL查询仅显示customerId的第一个结果,但count显示两个客户,mysql,Mysql,创建一个应用程序,其中该特定函数应在MySQL数据库中搜索每个特定月份都有约会的customerId客户,并在TableView的左栏显示customerId,在右栏显示客户在特定月份的约会次数(计数) 专注于12月的结果,代码似乎只获取customerId的第一个结果,但将12月的两次约会计数。例如,对于十二月,它应该在单独的行上显示customerId的3和4,每个行在十二月的计数中都有1。相反,customerId的结果是3,计数是2。下面是编写的DAO代码,JavaFX链接到MySQL

创建一个应用程序,其中该特定函数应在MySQL数据库中搜索每个特定月份都有约会的customerId客户,并在TableView的左栏显示customerId,在右栏显示客户在特定月份的约会次数(计数)

专注于12月的结果,代码似乎只获取customerId的第一个结果,但将12月的两次约会计数。例如,对于十二月,它应该在单独的行上显示customerId的3和4,每个行在十二月的计数中都有1。相反,customerId的结果是3,计数是2。下面是编写的DAO代码,JavaFX链接到MySQL

我确信这是SQL代码部分的一个问题,因为我对编码还不熟悉,但MySQL的技能就更少了。谢谢你的帮助

public static ObservableList<Report> getDecemberCustomers() {
        ObservableList<Report> monthCustomers = FXCollections.observableArrayList();
    try {
        PreparedStatement stmt = DBConnection.conn.prepareStatement( 
            "SELECT MONTHNAME(`start`) AS \"month\", customerId AS \"customerId\", COUNT(*) as \"count\" "
            + "FROM appointment "
            + "WHERE MONTH(start) = 12"
        );
            ResultSet rs = stmt.executeQuery();
            while (rs.next()) {
                Report monthCustomer = new Report();
                monthCustomer.setMonth(rs.getString("month"));
                monthCustomer.setCustomerId(rs.getInt("customerId"));
                monthCustomer.setCount(rs.getInt("count"));
                monthCustomers.add(monthCustomer);
            }
        } catch (SQLException e) {
            System.out.println("SQL error: " + e.getMessage());
        }
    return monthCustomers;
    }
publicstaticobserveListGetDecemberCustomers(){
ObservableList monthCustomers=FXCollections.observableArrayList();
试一试{
PreparedStatement stmt=DBConnection.conn.prepareStatement(
选择MONTHNAME(`start`)作为\“月\”,customerId作为\“customerId\”,COUNT(*)作为\“计数”
+“从预约开始”
+“其中月份(开始)=12”
);
ResultSet rs=stmt.executeQuery();
while(rs.next()){
报告月客户=新报告();
monthCustomer.setMonth(rs.getString(“月”));
monthCustomer.setCustomerId(rs.getInt(“customerId”);
月客户设置计数(rs.getInt(“计数”));
monthCustomers.add(monthCustomer);
}
}捕获(SQLE异常){
System.out.println(“SQL错误:+e.getMessage());
}
返回月客户;
}

您的查询缺少一个
GROUP BY
子句,因此它将所有客户的所有约会计算到一行中,并带有一个不确定的
customerId
值。您需要
按客户ID分组
,以获得每位客户的预约:

    PreparedStatement stmt = DBConnection.conn.prepareStatement( 
        "SELECT MONTHNAME(`start`) AS \"month\", customerId AS \"customerId\", COUNT(*) as \"count\" "
        + "FROM appointment "
        + "WHERE MONTH(start) = 12 "
        + "GROUP BY customerId"
    );
根据您的MySQL版本,您可能还需要将
month
添加到
groupby
中,即
groupby customerId,month