以下查询在 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/