java - 查询在 MySQL 提示符中执行,但不在应用程序中执行

标签 java mysql

以下查询在 MySQL 提示符下正确执行,但在 Java 中出现错误 (您的 SQL 语法有错误;请检查与您的 MySQL 服务器版本相对应的手册,了解在 'by 附近使用的正确语法(select avg( case Grade when 'A+' then 4 when 'A' then 4 when 'A -' 然后 3.7 w' 在第 1 行)

java.sql.ResultSet rslt= stmt.executeQuery("select name from student natural join takes t1 where (dept_name='Comp. Sci.')" +
        "group by ID" +
        "order by (select avg( case grade " +
                        "when 'A+' then 4 " +
                        "when 'A' then 4 " +
                        "when 'A-' then 3.7 " +
                        "when 'B+' then 3.5 " +
                        "when 'B' then 3 " +
                        "when 'B-' then 2.7 " +
                        "when 'C+' then 2.5 " +
                        "when 'C' then 2 " +
                        "when 'C-' then 1 " +
                        "end) as avg_gpa " +
                   "from takes t2 " +
               "where t1.ID=t2.ID) " +
        "limit 10");

有人可以帮忙吗?

最佳答案

不要忘记每行后面的空格:

java.sql.ResultSet rslt= stmt.executeQuery(
"select name from student natural join takes t1 where (dept_name='Comp. Sci.') "+
"group by ID "+
"order by (select avg( case grade "+
        "when 'A+' then 4  "+
    "when 'A' then 4 "+
    "when 'A-' then 3.7 "+
    "when 'B+' then 3.5 "+
    "when 'B' then 3 "+
    "when 'B-' then 2.7 "+
    "when 'C+' then 2.5 "+
    "when 'C' then 2 "+
    "when 'C-' then 1 "+
    "end) as avg_gpa "+
    "from takes t2 "+
"where t1.ID=t2.ID) "+
"limit 10");

你的sql是错误的:

"group by ID" +
        "order by (select avg( case grade " +

这里有一个像这样的字符串:“group by IDorder by (select avg( casegrade”

你看到问题了吗?

ID后添加一个空格

关于java - 查询在 MySQL 提示符中执行,但不在应用程序中执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20586672/

相关文章:

mysql - 如果列不存在,则将列添加到 mysql 表中

java - XWPFTableRow 添加具有当前样式的新行?

java - 如何在 Java 中声明一个 uint8_t 数组

java - JAXB/Moxy Unmarshalling 将所有字段值分配给 Map<String,Object>,而不是为其提供的特定字段

javascript - 返回 null mysql nodejs

php - 在 PHP 中使用 sprintf 进行 MySQL 查询

Mysql函数,返回多行

java - 从技术上讲,什么是数据库连接?

java - 更改 jPanel 中矩形的边框

java - eclipse indigo 中的可运行 jar 创建选项