select * from article where pid between 1 and 5 order by pdate desc 可以
select * from article where pid=0 between 1 and 5 order by pdate desc 不可以;
为什么加上条件pid=0就会显示between附件有错误;关于分页Top我百度看的不明白。有人能讲解下?
select * from article where pid=0 between 1 and 5 order by pdate desc 不可以;
为什么加上条件pid=0就会显示between附件有错误;关于分页Top我百度看的不明白。有人能讲解下?
select * from article where pid =0 between " +startPos+ " and " + pageSize+ "order by pdate desc。这个分页显示。就是找pid=0的值。显示startPos到pageSize个。可是SQL 2008这个话该怎么写呢? 我去找百度SQL2008的分页。看不懂。想用SQL2008实现这句语句。
select top (pagesize-startpos+1) * from article where pid =0
stratPos = (pageno-1)*pageSize;//pageSize每页显示多少条,pageno页数,
lastPos = pageno*pageSize;
Statement st=conn.createStatement();
ResultSet rs1 = st.executeQuery("select top(lastPos-stratPos) * from article where pid =0" );
代码如上。
Statement st=conn.createStatement();
ResultSet rs1 = st.executeQuery("select top(10-stratPos) * from article where pid =0" );
试试会不会报错,没环境不好调试
lastPos = pageno*pageSize;
Statement st=conn.createStatement();
ResultSet rs1 = st.executeQuery("select top('lastPos'-'stratPos') * from article where pid =0" );
会显示 操作数数据类型 varchar 对于 subtract 运算符无效。
。
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%@ page import="java.sql.*"%>
<%!int lastPos;
int stratPos; %>
<%
String Strpage =request.getParameter("page");
int pageno,lastpage,pageSize;
pageSize=3;
if((Strpage == null) || (Strpage.equals("")))
{
pageno=1;
}
else
{
try{
pageno=Integer.parseInt(Strpage.trim());
}catch(NumberFormatException e)
{
pageno=1;
}
if(pageno<=0) pageno =1;
}
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=bbs","sa","123123");
Statement ste=conn.createStatement();
ResultSet rs=ste.executeQuery("select count(*) from article where pid=0");
rs.next();
int totalRecords =rs.getInt(1);
rs.close();
ste.close();
int totalpage= totalRecords % pageSize==0 ?totalRecords/pageSize: totalRecords/pageSize+1;
if(pageno>totalpage)
pageno=totalpage;
stratPos = (pageno-1)*pageSize;//pageSize每页显示多少条,pageno页数,
lastPos = pageno*pageSize;
Statement st=conn.createStatement();
ResultSet rs1 = st.executeQuery("select top(lastPos-stratPos)*from article where pid =0");
%>
<html>
<head>
</head>
<body>
<a href ="Post.jsp">发新帖</a>
<table border="1">
<% while(rs1.next()){ %>
<tr>
<td>id</td><td><%=rs1.getInt("id") %></td>
<td>Title</td><td><%=rs1.getString("title") %></td>
</tr>
<%}
rs1.close();
st.close();
conn.close();
%>
</table>
共<%=totalpage %> 页.第<%=pageno %>页
<a href ="ShowArticleFlat.jsp?pageno=<%=1%>">首页</a>
<a href ="ShowArticleFlat.jsp?pageno=<%=pageno-1 %>">上一页</a>
<a href ="ShowArticleFlat.jsp?pageno=<%=pageno+1 %>">下一页</a>
<a href ="ShowArticleFlat.jsp?pageno=<%=totalpage %>">最后一页</a>
</body>
</html>全部代码。
试试这个:ResultSet rs1 = st.executeQuery("select top("+lastPos+"-"+stratPos+")*from article where pid =0");
你里面已经不是一个变量,所以SQLServer觉得是一个列名