string sql = "select * from Article where 1=1";if(BlogID!=0)sql+= " and BlogID = " + BlogID;
if(CategoryID!=0)sql+= " and CategoryID = " + CategoryID;
if(key!="")sql += " and (Subject like '%" + key + "%' or Content like '%" + key + "%'";
if(time!="")sql += " and [time] like '%" + time + "%'";sql += " order by ArticleID desc";第一行的where 1=1 是什么意思啊

解决方案 »

  1.   

    判断咯,其实个人感觉没什么意义,如果是1=a,a在外面定义的话还有意义
    比如说,如果a=1,那么就是1=1咯,说明这条语句一定会被执行,如果a为1以外其他的数,比如是2,那么where 1=a就成了where 1=2,显然,这是不相等的,所以得出的结果就是空的咯,大概是这样吧.
      

  2.   

    加上“where 1=1”只是为了后面拼凑语句更方便一些
    ===
    if(BlogID!=0)sql+= " and BlogID = " + BlogID;
    if(CategoryID!=0)sql+= " and CategoryID = " + CategoryID;
    if(key!="")sql += " and (Subject like '%" + key + "%' or Content like '%" + key + "%'";
    if(time!="")sql += " and [time] like '%" + time + "%'";如果不用“where 1=1”,你怎么知道上面的四个if中哪个该写上“where”呢?
      

  3.   

    where 1=1 查出所有的数据!
      

  4.   

    为了写起来方便,不需要在后面都为假时判断 去掉 个 Where