String sql = "select * from msgInfo where ID in(select top 15 ID from msgInfo where chatRoom=? Order by chatTime DESC) order by chatTime";
String userName=session.getAttribute("_USER").toString();
PreparedStatement ps = conn.preparedStatement(sql);
ps.setString(1,session.getAttribute("_CHAT_ROOM").toString());
ResultSet rs = conn.executeQuery();
这语句有问题吗,怎么老报错,没道理啊
ou have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '15 ID from msgInfo where chatRoom='dragon inn' Order by chatTime DESC) order by ' at line 1
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '15 ID from msgInfo where chatRoom='dragon inn' Order by chatTime DESC) order by ' at line 1
我试到了正确的答案 SELECT*FROMmsginfoWHEREIDIN(SELECTIDFROMmsginfoWHEREchatRoom='dragoninn'ORDERBYchattimeDESC )ORDERBYchattimeLIMIT15;
报错很明显了,15IDfrommsgInfowherechatRoom='dragoninn'OrderbychatTimeDESC)orderby'atline1
可能是你用的数据库不支持top
你换成这样应该可以成功select*frommsgInfowhereIDin(selectIDfrommsgInfowherechatRoom=?OrderbychatTimeDESClimit15)orderbychatTime
即将top15换成在尾部添加limit15
回复 @助哥的后花园:我公司的时间一般设置成14位的字符串,如:"20160510152300",这种是可以排序的,我不确定datetime类型是否可以排序,你可以去掉排序,然后试下。回复 @求是科技:我上了图片,是不是datatime不能排序..纯sql报错不?抛开传参,你将这段SQL放到sql客户端里面执行,看是否报错,然后根据报错信息拍错limit不支持,TOP支持可是都报错 试试这个select*frommsgInfowhereIDin(selectIDfrommsgInfowherechatRoom=?OrderbychatTimeDESC top15)orderbychatTime这样写试试看:
select类别,sum(数量)as数量之和,摘要
fromA
groupby类别,摘要
orderby类别desc
如果仍报错请参考资料: http://edu.51cto.com/course/course_id-1107.html
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。