在myeclipse执行mssql
String sql = "select top 10 * from topic where boardId=" + boardId
+ " and topicId not in(select top " + rowBegin
+ " topicId from topic where boardId=" + boardId
+ " order by publishTime desc)order by publishTime desc";这是SQL是可以通过
可以通过,那要改写成mysql数据库的语法如下代码所示:
int startCount = 0;
int pageSize = 10;
if (page > 1) {
startCount = 10 * (page - 1);
}
String sql = "select * from topic order by publishTime desc limit "
+ startCount
+ ","
+ pageSize
+ " where boardId="
+ boardId+" and topicId not in(select topicId from tpoic order by publishTime desc limit " + startCount
+ "," + pageSize + "where boardId="+ boardId +")";
上面的代码有什么问题呢,在mysql里面怎么执行出来总是报这样的错
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 'where boardId=8 and topicId not in(select topicId from tpoic order by publishTim' at line 1
我传入的boardId是8。
怎么改啊,请求帮助,不知道怎么折腾了。
------解决方案--------------------------------------------------------
where放在from子句后面试试
------解决方案--------------------------------------------------------
LIMIT 语句放最后。
------解决方案--------------------------------------------------------
String sql = "select * from topic where boardId="
+ boardId+" order by publishTime desc limit "
+ startCount
+ ","
+ pageSize
+ " and topicId not in(select topicId from tpoic where boardId="+ boardId +" order by publishTime desc limit " + startCount
+ "," + pageSize + ")";
------解决方案--------------------------------------------------------
String sql = "select * from topic where boardId="
+ boardId
+ " and topicId not in(select topicId from tpoic where boardId="+ boardId +" order by publishTime desc limit " + startCount
+ "," + pageSize + ")"
+" order by publishTime desc limit "
+ startCount
+ ","
+ pageSize;
------解决方案--------------------------------------------------------
String sql = "select * from topic where boardId="
+ boardId
+ " and topicId not in(select topicId from (select * from topic where boardId="
+ boardId + " order by publishTime desc limit " + startCount
+ "," + pageSize + ") as t)" + " order by publishTime desc limit "
+ startCount + "," + pageSize;