2012-07-10 62 views
1

我需要在最近的日期订购我的数据。SQL语句(排序)

这个SQL statment是工作

final String selectSql = "select * from questionnaire where userprofileid=" + userProfileID ; 

这个SQL statment是** 不工作**

final String selectSql = "select * from questionnaire where userprofileid=" + userProfileID +"ORDER by datecreated desc"; 

错误消息:
com.mysql.jdbc.exceptions .jdbc4.MySQLSyntaxErrorException:您的SQL语法中有错误;检查对应于你的MySQL服务器版本“由dateCreated会递减”的线附近使用正确的语法手册1

+0

除了空间的问题其他的答案已经指出的那样,请不要建立动态SQL这样。改为使用参数化查询,这将有助于避免SQL注入攻击。 – 2012-07-10 03:02:23

回答

2

您需要订单之前加一个空格:

final String selectSql = "select * from questionnaire where userprofileid=" + userProfileID +" ORDER by datecreated desc"; 
+0

谢谢:)它现在运作 – 2012-07-10 02:56:30

1

请尝试离开ORDER BY之前的空格。 " ORDER BY datecreated desc"

+0

谢谢:)它现在有效 – 2012-07-10 02:57:14

+0

@ AnneMahLi'en请接受一些以前回答的问题,以提高您的速度。 – 2012-07-10 03:04:10

1

最终的字符串应该是这样的:

final String selectSql = "select * 
          from questionnaire 
          where userprofileid=" + userProfileID + 
          " ORDER by datecreated desc"; // add a space 
                 // after the double 
                 // quote 
+0

谢谢:)它现在有效 – 2012-07-10 02:57:51