string strSQL = "select * from member where sexs=?sexs or age>=?minage and age<=?maxage or continent=?continent or country=?country or location=?location";
我想要的结果是:
       当我选择了性别,其他没有选择,就按性别查询,如果,添加了一个条件,比如说年龄,那么,就有两个条件根据性别和年龄,同时满足两个条件查询。以此类推...如果,全选择就按全部条件..  请问这个sql语句要怎么写?表述不详细的地方,请说 我现在写的sql,没有达到我想要的结果。。能够分开查询,但是,同时选择两个条件以上,那么,两个条件的东西都会查询出来...

解决方案 »

  1.   

    string strsql="select * from biao where 1=1 ";
    if(strAge!="")
    {
    strsql+=" and age="+strage;
    }
    ...省略
      

  2.   

    StringBuilder的拼接而已,if来判断条件是否为空;与还是或的关系,继续嵌套if判断就可。
      

  3.   

    我觉得用判断,太多代码,不精简,有没有别的办法?比如,sql语句能够达到的效果?
      

  4.   

    晕...你sql里面都是or关系,不用判断了,直接这么写就行了
      

  5.   

    select * from member 
    where (sexs=?sexs or ?sexs = 默认)
    and (age = ?age or ?age = 默认)查询条件输入时 根据查询条件,未输入时,给定默认值 
    例 age 默认为0 
    and(age=25 or 25 = 0)
    and(age = 0 or 0 = 0)
      

  6.   


    能帮我上面那个sql改一下吗?,就改两个条件,我看的不是太懂
      

  7.   

    string sql = "SELECT (SELECT sum(ProPrice*ProNum) FROM Hd_OrderProducts where OrderNum =a.OrderNum) as pice,((SELECT sum(ProPrice*ProNum) FROM Hd_OrderProducts where OrderNum =a.OrderNum)+Freight) HJpices,Id,UserName,a.OrderNum,Consignee,Mobile,TelePhone,Email,PostCode,Prov,City,Area,Address " +
                             " BestSendTime,Payment,Invoice,Message,DeliveryMode,CreateDate,isStatus,deliveryStatus,transactionState,CashStatus,isAudit FROM Hd_Orders as a ";
                #region
                if ((od.IsStatus != "" && od.IsStatus != null) || (od.CashStatus != "" && od.CashStatus != null) || (od.DeliveryStatus != "" && od.DeliveryStatus != null)
                    || (od.TransactionState != "" && od.TransactionState != null) || (od.OrderNum != "" && od.OrderNum != null) || (od.TelePhone != "" && od.TelePhone != null)
                    || (od.SelectOneTime != "" && od.SelectOneTime != null) || (od.SelectTwoTime != "" && od.SelectTwoTime != null) || (od.IsAudit != "" && od.IsAudit != null))
                {
                    sql += "where ";                if (od.IsStatus != "" && od.IsStatus != null)
                    {
                        sql += "a.IsStatus=@IsStatus ";
                    }                if (od.CashStatus != "" && od.CashStatus != null)
                    {
                        if (od.IsStatus != "" && od.IsStatus != null)
                        {
                            sql += " and ";
                        }
                        sql += "a.CashStatus=@CashStatus ";
                    }                if (od.DeliveryStatus != "" && od.DeliveryStatus != null)
                    {
                        if ((od.IsStatus != "" && od.IsStatus != null) || (od.CashStatus != "" && od.CashStatus != null))
                        {
                            sql += " and ";
                        }
                        sql += "a.DeliveryStatus=@DeliveryStatus ";
                    }                if (od.TransactionState != "" && od.TransactionState != null)
                    {
                        if ((od.IsStatus != "" && od.IsStatus != null) || (od.CashStatus != "" && od.CashStatus != null) || (od.DeliveryStatus != "" && od.DeliveryStatus != null))
                        {
                            sql += " and ";
                        }
                        sql += "a.TransactionState=@TransactionState";
                    }                if (od.OrderNum != "" && od.OrderNum != null)
                    {
                        if ((od.IsStatus != "" && od.IsStatus != null) || (od.CashStatus != "" && od.CashStatus != null) || (od.DeliveryStatus != "" && od.DeliveryStatus != null) || (od.TransactionState != "" && od.TransactionState != null))
                        {
                            sql += " and ";
                        }
                        sql += "a.OrderNum=@OrderNum ";
                    }                if (od.TelePhone != "" && od.TelePhone != null)
                    {
                        if ((od.IsStatus != "" && od.IsStatus != null) || (od.CashStatus != "" && od.CashStatus != null) || (od.DeliveryStatus != "" && od.DeliveryStatus != null)
                            || (od.TransactionState != "" && od.TransactionState != null) || (od.OrderNum != "" && od.OrderNum != null))
                        {
                            sql += " and ";
                        }
                        sql += "a.Mobile=@TelePhone or a.TelePhone=@TelePhone ";
                    }                if ((od.SelectOneTime != "" && od.SelectOneTime != null) || (od.SelectTwoTime != "" && od.SelectTwoTime != null))
                    {
                        if ((od.IsStatus != "" && od.IsStatus != null) || (od.CashStatus != "" && od.CashStatus != null) || (od.DeliveryStatus != "" && od.DeliveryStatus != null)
                            || (od.TransactionState != "" && od.TransactionState != null) || (od.OrderNum != "" && od.OrderNum != null) || (od.TelePhone != "" && od.TelePhone != null))
                        {
                            sql += " and ";
                        }
                        sql += "a.CreateDate>@SelectOneTime and a.CreateDate<@SelectTwoTime ";
                    }                if (od.IsAudit != "" && od.IsAudit != null)
                    {
                        if ((od.IsStatus != "" && od.IsStatus != null) || (od.CashStatus != "" && od.CashStatus != null) || (od.DeliveryStatus != "" && od.DeliveryStatus != null)
                            || (od.TransactionState != "" && od.TransactionState != null) || (od.OrderNum != "" && od.OrderNum != null) || (od.TelePhone != "" && od.TelePhone != null)||
                            (od.SelectOneTime != "" && od.SelectOneTime != null) || (od.SelectTwoTime != "" && od.SelectTwoTime != null))
                        {
                            sql += " and ";
                        }
                        sql += "a.IsAudit=@IsAudit ";
                    }            }
                #endregion
      

  8.   

    楼主考虑过 多谢几个SQL语句吗拼接是可以 效率不是很好
      

  9.   

    你的sql 条件语句太烦了  应该整的 明了点  这样 有利于系统的识别
      

  10.   

    用if...else if...else多重条件判断语句来写sql语句
      

  11.   

    string sql="select * from 表 where 1=1";
    if(性别文本.length>0)
    {
       sql +=" and 性别='"+性别文本+"'";
    }
    if(年龄文本.length>0)
    {
      sql +=" and 年龄='"+年龄文本+"'";
    }
    .......以此类推
      

  12.   

    你这样写能OK吗?
    比如:
    if(性别文本.length>0)
    {
      sql +=" and 性别='"+性别文本+"'";
    }
    你性别选择了.但其他的呢查询出来?还有一种情况,我选择了性别,国家,地区,其他不选择,你这样写能够实现只查询这个3个条件,其他不查询码?
      

  13.   


    string strSQL = "select * from member where sexs=?sexs or age>=?minage and age<=?maxage or continent=?continent or country=?country or location=?location";
    能我把这个sql语句改一下吗?我能看的懂一点、、
      

  14.   

    封装到类里面,像这样:Class1 c1 = new Class1();
    ClassSearchOption cso1 = new ClassSearchOption();//用它来构造查询条件DataTable data = c1.Search(cso1);