【问题标题】:Utilisation invalide de la clause GROUP使用无效 de la 子句 GROUP
【发布时间】:2020-04-22 02:04:13
【问题描述】:

我在尝试对 MySql 数据库表中的一行(记录)中的分数求和时遇到错误。我想要实现的是更新term1 表中的Total_Score 列。如果任何分数发生变化,那么我必须相应地更新Total_Score 列。下面是给出错误的部分编码。

String qry2 = null;
Prepared statement ps2 = null;

qry2 = " UPDATE term1 SET Total_Score = sum(English + Social_Science + Science  + Maths + PE + MAL Arts) WHERE SID = ?";

ps2.=conn.prepareStatement(qry2);
Int res2=ps2.executeUpdate();

【问题讨论】:

  • 那是您的准确代码吗?
  • 或者你可以有一个generated column 而不需要update 声明。
  • 修复损坏的架构。数据库表不是电子表格。在规范化环境中,您不会有单独的表用于单独的术语,也不会有单独的列用于单独的主题。相反,您可能有一个学生列、一个学期列、一个学科列和一个标记列。

标签: java mysql netbeans


【解决方案1】:

该代码中有很多错误:

String qry2 = null;
Prepared statement ps2 = null; // Data type is `PreparedStatement`, not `Prepared statement`

// Don't call `sum()` unless you're summing multiple rows, and you're not.
// What is `MAL Arts`? Column names cannot have spaces, unless you quote
//   the name, and you really don't want to be doing that.
qry2 = " UPDATE term1 SET Total_Score = sum(English + Social_Science + Science  + Maths + PE + MAL Arts) WHERE SID = ?";

ps2.=conn.prepareStatement(qry2); // no period before =
Int res2=ps2.executeUpdate(); // Data type is `int`, not `Int`

看起来你的代码应该是:

String qry2 = "UPDATE term1" +
             " SET Total_Score = English + Social_Science + Science + Maths + PE + MALArts" +
             " WHERE SID = ?";
int res2;
try (PreparedStatement ps2 = conn.prepareStatement(qry2)) {
    ps2.setInt(1, sid);
    res2 = ps2.executeUpdate();
}
// code using `res2` here

【讨论】:

  • 这反而忽略了房间里的大象。!
猜你喜欢
  • 1970-01-01
  • 2022-06-14
  • 1970-01-01
  • 2021-06-15
  • 1970-01-01
  • 2014-07-24
  • 1970-01-01
  • 2022-10-25
  • 2011-09-25
相关资源
最近更新 更多