Select * From myTable Where 1=1

解决方案 »

  1.   

    Select * From myTable Where 表单字段=条件,where 之後是接条件式,把符合条件的资料列出来。
      

  2.   

    建议去掉,影响效率,又不直观。
    你可以把where 的信息写入一个string,如果你的where是没有东西的,让他为空,否则就加上原来的SQL语句
    string sSQL, sCondition;
    sSQL = "Select * From myTable"
    if (需要条件判断)
      sCondition = "where " + "a=" + a.ToString();sSQL += sCondition;
      

  3.   

    where 后面加一个索引字段字段,而且不为空就可以了
    如: select * from mytable where field<>''
      

  4.   

    to  greenery(greenery) 
    Select * From myTable Where (1=1)
    这个怎样影响效率呢?
    我觉得这个写法是非常有用的,当我们查询的时候,有多个判断条件,就可以这样
    sql="Select * From myTable Where (1=1)"
    if(a!="")
     sql+=" and a=**";
    if(b!="")
      sql+=" and b=**";
    如果没有用1=1,我们就要这样
    if(a!="")
    sql+=" a=**";
    if(a==""&&b!="")
    sql+=" b=**";
    if(a!=""&&b!="")
    sql+" and b=**";
    当你有多个判断条件的时候,就不好处理了
      
      

  5.   

    我一般会这么写string sSql = "select * from myTable";
    string sSqlCondition = String.Empty;
    string sAnd = String.Empty;if (需要Checked)
    {
      sSqlCondition += sAnd + "(res.[Checked] = 1) AND (res.[Open] = 1) ";
      sAnd = " AND ";
    }
    if (需要Share)
    {
      sSqlCondition += sAnd + " (res.[Share] = 1) ";
      sAnd = " AND ";
    }if (sSqlCondition.Length > 0)
      sSqlCondition += " where ";sSql =+ sSqlCondition;
    // 最后执行sSql 
    我觉得这样生成的SQL语句清楚些。
    请各位指教