日期:2014-05-17  浏览次数:20471 次

SQL查询语句怎么写?
表test以下信息:
lid lname lpass ltouxiang lxingbie
7 营营 123 3.gif 男
8 超超 123 4.gif 男
9 宏宏 123 1.gif 男
10 佳佳 123 5.gif 男
11 强强 123 2.gif 男
16 龙龙 123 1.gif 女
17 志志 123 6.gif 女
18 顺顺 123 1.gif 女
19 明明 123 7.gif 男

给出5个条件lid,lname,lpass,ltouxiang,lxingbie。
怎么查询出只要满足以上任意3个条件SQL语句?

------解决方案--------------------
给出5个条件lid,lname,lpass,ltouxiang,lxingbie。
怎么查询出只要满足以上任意3个条件SQL语句?
----------
这是5个字段名,怎么会是5个条件呢?
是不是说lid=@lid,lname=@lname,lpass=@lpass,ltouxiang=@ltouxiang,lxingbie=@lxingbie。这样的5个条件?
如果是,就好办,将这5个条件放进一个数组,然后对这个数组进行5选3的所有组合全部列出来即可。
0 lid=@lid
1 lname=@lname
2 lpass=@lpass
3 ltouxiang=@ltouxiang
4 lxingbie=@lxingbie
C# code


    protected void Button1_Click(object sender, EventArgs e)
    {
        string strSQL = "Select lid lname lpass ltouxiang lxingbie From Test Where " + getCondition;
        //DataTable dt = getQuery(strSQL);执行查询语句。

    }
    private string getCondition()
    { 
        //初始化数组
        string[] cCons = "lid=@lid,lname=@lname,lpass=@lpass,ltouxiang=@ltouxiang,lxingbie=@lxingbie".Split(',');
        string cCondition = "(1=0 ";

        for (int iC0 = 0; iC0 < cCons.Length; iC0++)
        {
            for (int iC1 = 0; iC1 < cCons.Length; iC1++)
            {
                if (iC1 == iC0) continue;
               
                for (int iC2 = 0; iC2 < cCons.Length; iC2++)
                {
                    if (iC2 == iC0 || iC2 == iC1) continue;
                    cCondition += "\n OR ( " + cCons[iC0] +  " And " + cCons[iC1]+ " And " + cCons[iC2]+ ")";
                }
            }
       
        }
        cCondition += "\n)";
        return cCondition;
    }

------解决方案--------------------
上面打错了,又不给改,该死的网站。

代码重新贴:
C# code


 protected void ExecMyQuery(string lid,string lname,string lpass ,string ltouxiang ,string lxingbie )
    {
        string strSQL = @"Declare @lid nvarchar(300), @lname nvarchar(300), @lpass  nvarchar(300), @ltouxiang  nvarchar(300), @lxingbie  nvarchar(300)";
        strSQL += "Select @lid='" + txtLid + "',@lname='" + lname + "',@lpass='" + lpass + "',@ltouxiang='" + ltouxiang + "',@lxingbie='" + lxingbie + "'";
        strSQL += "Select lid lname lpass ltouxiang lxingbie From Test Where " + getCondition();

        //DataTable dt = getQuery(strSQL);

    }

    private string getCondition()
    { 
        //初始化数组
        string[] cCons = "lid=@lid,lname=@lname,lpass=@lpass,ltouxiang=@ltouxiang,lxingbie=@lxingbie".Split(',');
        string cCondition = "(1=0 ";

        for (int iC0 = 0; iC0 < cCons.Length; iC0++)
        {
            for (int iC1 = 0; iC1 < cCons.Length; iC1++)
            {
                if (iC1 == iC0) continue;
               
                for (int iC2 = 0; iC2 < cCons.Length; iC2++)
                {
                    if (iC2 == iC0 || iC2 == iC1) continue;
                    cCondition += "\n OR ( " + cCons[iC0] +  " And " + cCons[iC1]+ " And " + cCons[iC2]+ ")";
                }
            }
       
        }
        cCondition += "\n)";
        return cCondition;
    }

------解决方案--------------------
探讨
select *
from table
where 1=1
and (
case when condition1(@arg1) is true then 1 else 0 end
+ case when condition2(@arg2) is true then 1 else 0 end
+ case when condition3(@arg3) is true then 1……

------解决方案--------------------