生成报告时GROUP BY子句问题

JWingert:

我正在使用SQL数据库在IntelliJ中编写Java应用程序以进行约会处理。对于一部分,我必须生成3种类型的报告:按月列出的约会类型数,每个顾问的时间表以及今年的约会总数。我以前可以进行此工作(并且没有做任何更改),现在它在前两个报告生成中返回错误:

Error Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'U07sym.appointment.start' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

Error Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'U07sym.appointment.description' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

我尝试了一些建议,但它们似乎需要“超级”访问权限,并且由于这是我学校托管的数据库,因此我没有该访问权限。

这是1,2和3报告生成器的代码:

public void generateFirstReport() {

    try {
        
        Statement statement = DBConnection.getConnection().createStatement();
        String appointmentTypeQuery = "SELECT description, MONTHNAME(start) as 'Month', COUNT(*) as 'Total' FROM appointment GROUP BY description, MONTH(START)";
        
        ResultSet appointmentQueryResults = statement.executeQuery(appointmentTypeQuery);
        
        StringBuilder parseReport = new StringBuilder();
        
        parseReport.append(String.format("%1$-60s %2$-60s %3$s \n", "Month", "Appointment Type", "Total"));
        parseReport.append(String.join("", Collections.nCopies(163, "-")));
        parseReport.append("\n");
        
        while(appointmentQueryResults.next()) {
            
            parseReport.append(String.format("%1$-56s %2$-60s %3$s \n", appointmentQueryResults.getString("Month"), appointmentQueryResults.getString("description"), appointmentQueryResults.getInt("Total")));
        }
        
        typesOfMonthsText.setText(parseReport.toString());
    }
    
    catch(SQLException ex) {
        System.out.println("Error " + ex.getMessage());
    }
}
    
public void generateSecondReport() {
    
    try {
        
        Statement statement = DBConnection.getConnection().createStatement();
        String consultantQueryResults = "SELECT appointment.contact, appointment.description, customer.customerName, start, end " + "FROM appointment JOIN customer ON customer.customerId = appointment.customerId " +
                "GROUP BY appointment.contact, MONTH(start), start";
        
        ResultSet appointmentQueryResults = statement.executeQuery(consultantQueryResults);
        
        StringBuilder parseReport = new StringBuilder();
        parseReport.append(String.format("%1$-45s %2$-45s %3$-45s %4$-45s %5$s \n", "Consultant", "Appointment", "Customer", "Start", "End"));
        parseReport.append(String.join("", Collections.nCopies(163, "-")));
        parseReport.append("\n");
        
        while(appointmentQueryResults.next()) {
            
            parseReport.append(String.format("%1$-37s %2$-50s %3$-35s %4$-35s %5$s \n", 
                appointmentQueryResults.getString("contact"), appointmentQueryResults.getString("description"), appointmentQueryResults.getString("customerName"),
                appointmentQueryResults.getString("start"), appointmentQueryResults.getString("end")));
        }
        
        scheduleOfConsultantText.setText(parseReport.toString());         
        
    }
    catch(SQLException ex) {
       System.out.println("Error " + ex.getMessage()); 
    }       
}

public void generateThirdReport () {
    
    try {
        Statement statement = DBConnection.getConnection().createStatement();
        //"SELECT description, MONTHNAME(start) as 'Month', COUNT(*) as 'Total' FROM appointment GROUP BY description, MONTH(START)";
        String appointmentQueryResults = "SELECT YEAR(start) as 'Year', COUNT(*) as 'Total' FROM appointment GROUP BY YEAR(start)";
        
        ResultSet yearlyQueryResults = statement.executeQuery(appointmentQueryResults);
        
        StringBuilder parseReport = new StringBuilder();
        parseReport.append(String.format("%1$-50s %2$-50s \n", "Year", "Total"));
        parseReport.append(String.join("", Collections.nCopies(163, "-")));
        parseReport.append("\n");
        
        while(yearlyQueryResults.next()) {
            
            parseReport.append(String.format("%1$-50s %2$-50s \n", yearlyQueryResults.getString("Year"), yearlyQueryResults.getString("Total")));
        }
        
        totalAppointmentsThisYearText.setText(parseReport.toString());
    }
    
    catch(SQLException ex) {
        System.out.println("Error " + ex.getMessage());
    }
}
Vivek Jain:

您的mysql当前仅支持only_full_group_by,这意味着select子句中出现的所有列都必须在诸如sum,avg,max等的组函数中或在group子句中。

要解决此问题,您必须更改mysql设置。

如果您正在使用mysql workbench,Windows则转到Edit >> Preferences,然后MySqlModeling

请参阅下面的屏幕快照以供参考。

在此处输入图片说明

之后,从MSQ_MODE to be used in generated scripts文本框中删除only_full_group_by,然后单击“确定”。然后重启你的mysql。

如果您使用的是Linux,则从Linux终端连接到mysql。

执行以下脚本。

SET sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));
SET GLOBAL sql_mode = 'STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章