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我百度看的不明白。有人能讲解下?

解决方案 »

  1.   

    select  * from article where pid=0 and pid between 1 and 5 order by pdate desc  少了红字的部分,但是你这句就算加上去都没数据,因为pid不可能同时等于0,又处于1~5之前。
      

  2.   

    除非你是别的东西between 1 and 5
      

  3.   

     我想问你个问题。Mysql 
    select * from article where pid =0 between " +startPos+ " and " + pageSize+ "order by pdate desc。这个分页显示。就是找pid=0的值。显示startPos到pageSize个。可是SQL 2008这个话该怎么写呢?      我去找百度SQL2008的分页。看不懂。想用SQL2008实现这句语句。
      

  4.   

    mysql很少很少用,但是你这个放到SQLServer必定报错。你试试下面的:
    select top (pagesize-startpos+1) * from article where pid =0
      

  5.   

    数据库可以成功。可是我放到JSP程序段里就显示出 列名 'lastPos' 无效。;
     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" );
    代码如上。
      

  6.   

    stratPos = (pageno-1)*pageSize;//pageSize每页显示多少条,pageno页数,
         Statement st=conn.createStatement();
         ResultSet rs1 = st.executeQuery("select top(10-stratPos) * from article where pid =0" );
    试试会不会报错,没环境不好调试
      

  7.   

    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" );
    会显示 操作数数据类型 varchar 对于 subtract 运算符无效。
      

  8.   

     改了之后变成stratPos无效了。。
      

  9.   

    那我的猜想就对了,你的这些:stratPos = (pageno-1)*pageSize;都没做定义,或者说定义的生命周期在传进去之前就结束了。
      

  10.   

     我确定定义了变量。至于生命周期应该没完结的。
    <%@ 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>全部代码。
      

  11.   

    ResultSet rs1 = st.executeQuery("select top(lastPos-stratPos)*from article where pid =0");
    试试这个:ResultSet rs1 = st.executeQuery("select top("+lastPos+"-"+stratPos+")*from article where pid =0");
    你里面已经不是一个变量,所以SQLServer觉得是一个列名