SQL查询语句怎么写?
表test以下信息:
lid lname lpass ltouxiang lxingbie
7营营1233.gif男
8超超1234.gif男
9宏宏1231.gif男
10佳佳1235.gif男
11强强1232.gif男
16龙龙1231.gif女
17志志1236.gif女
18顺顺1231.gif女
19明明1237.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的所有组合全部列出来即可。
0lid=@lid
1lname=@lname
2lpass=@lpass
3ltouxiang=@ltouxiang
4lxingbie=@lxingbie
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; }
[解决办法]
上面打错了,又不给改,该死的网站。
代码重新贴:
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; }
[解决办法]
where
(
case when 条件 then 1 else 0 end+
case when 条件 then 1 else 0 end+
case when 条件 then 1 else 0 end+
case when 条件 then 1 else 0 end+
case when 条件 then 1 else 0 end)
>=3 给楼主点我的思路 不碰SQLN年 看看行不
[解决办法]