[英]GROUP BY clause issue when generating reports
我正在使用 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());
}
}
您的 mysql 當前僅支持 only_full_group_by 意味着 select 子句中存在的所有列必須在 sum、avg、max 等組函數中或在 group 子句中。
要解決此問題,您必須更改 mysql 設置。
如果您在Windows
中使用 mysql 工作台,然后轉到Edit >> Preferences
,然后轉到 select MySql
下Modeling
請參閱下面的屏幕截圖以供參考。
之后從MSQ_MODE to be used in generated scripts
文本框中使用,然后單擊確定。 然后重啟你 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';
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.