重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
select * from table where (name1 like '%key1%' or name2 like '%key2%' or name3 like '%key3%')
创新互联建站长期为1000+客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为梁山企业提供专业的网站制作、成都网站建设,梁山网站改版等技术服务。拥有10余年丰富建站经验和众多成功案例,为您定制开发。
同理,你还可以任意再次组合,用or连接就行
如再上 name1 like key1 or name1 like key2 or name1 like key3
name2 like key1 or name2 like key2 or name2 like key3
等等
给你一个稍微复杂一点的查询,我设计的
看上图我把第一列标签后面的控件命名有规律,比如计划工厂后面文本框是"计划工厂t",Exapt命名为"计划工厂c",后面还隐藏一个listbox,命名为"计划工厂l"。
计划工厂 这个名称本身就是我要查询的表里面包含的字段。
利用这种界面,我要完成一些复杂点的查询:点文本框里的小图标按钮,弹出工具,可以输入多条件,条件可以成立为模糊条件(用*代替不认识部分),也可以成立为否决条件。
选中Exapt全部否定所成立条件;点击执行按钮,有条件的成立条件,无条件的不成立。
所以这种东西贯穿软件我就不能每个都去做,就只能写一个类文件:
public class conditionset
{
public void load_condit(Panel p)
{
foreach (Control ctl in p.Controls)
{
if (ctl is SkinTextBox)
{
SkinTextBox sktxt = (SkinTextBox)p.Controls[ctl.Name];
ListBox list = (ListBox)p.Controls[sktxt.Name.Substring(0, sktxt.Name.Length - 1) + "l"];
sktxt.IconClick += (Object sen, EventArgs ed) =
{
ctl.condition toolform = new ctl.condition();
if (list.Items.Count 0)
{
sktxt.Text = "┅";
sktxt.ReadOnly = true;
}
foreach (var sm in list.Items)
{
toolform.dv.Rows.Add(sm.ToString());
}
toolform.Show();
toolform.cleari += (Object send, EventArgs er) =
{
sktxt.Text = "";
sktxt.ReadOnly = false;
};
toolform.subm += (Object send, EventArgs er) =
{
list.Items.Clear();
foreach (DataGridViewRow dvr in toolform.dv.Rows)
{
if (Convert.ToString(dvr.Cells[0].Value) != "")
{
list.Items.Add(Convert.ToString(dvr.Cells[0].Value));
}
}
sktxt.Text = "┅";
sktxt.ReadOnly = true;
};
};
}
}
}
public string condit(Panel p)
{
string master_condition = "";
foreach (Control ctl in p.Controls)
{
//遍历panel查找条件
#region
string condition = "";
if (ctl is SkinTextBox)
{
//文本框条件组合
#region
SkinTextBox sktxt = (SkinTextBox)p.Controls[ctl.Name];
string ziduan_name = sktxt.Name.Substring(0, sktxt.Name.Length - 1);
SkinCheckBox skck = (SkinCheckBox)p.Controls[ctl.Name.Substring(0, ctl.Name.Length - 1) + "c"];
ListBox list = (ListBox)p.Controls[ctl.Name.Substring(0, ctl.Name.Length - 1) + "l"];
if (sktxt.Text != "")
{
if (sktxt.Text == "┅")
{
string blur_str = "", blur = "";
foreach (var itm in list.Items)
{
if (itm.ToString().Contains("*"))
{
if (skck.Checked == true)
{
blur += " and " + ziduan_name + " not like '" + itm.ToString().Replace("*", "%") + "'";
}
else
{
blur += " or " + ziduan_name + " like '" + itm.ToString().Replace("*", "%") + "'";
}
}
else
{
blur_str += "'" + Convert.ToString(itm) + "',";
}
}
string blur_sql = (blur == "") ? "" : blur.Substring(4, blur.Length - 4).Trim();
string in_condition = "", like_condition = "";
if (skck.Checked == true)
{
in_condition = ziduan_name + " not in ";
like_condition = " and ";
}
else
{
in_condition = ziduan_name + " in ";
like_condition = " or ";
}
string contains_sql = (blur_str == "") ? "" : in_condition + "(" + blur_str.Substring(0, blur_str.Length - 1) + ")";
condition = contains_sql + blur_sql;
if (blur_sql != "" contains_sql != "")
{
condition = contains_sql + like_condition + blur_sql;
}
else
{
condition = contains_sql + blur_sql;
}
}
else
{
if (!sktxt.Text.Contains("*"))
{
if (skck.Checked == true)
{
condition = ziduan_name + "'" + sktxt.Text + "'";
}
else
{
condition = ziduan_name + "='" + sktxt.Text + "'";
}
}
else
{
if (skck.Checked == true)
{
condition = ziduan_name + " not like '" + sktxt.Text.Replace("*", "%") + "'";
}
else
{
condition = ziduan_name + " like '" + sktxt.Text.Replace("*", "%") + "'";
}
}
}
}
#endregion
}
if (ctl is SkinDateTimePicker)
{
//日期条件组合
#region
if (ctl.Name.Substring(ctl.Name.Length - 1, 1) != "t")
{
SkinDateTimePicker skdate_sta = (SkinDateTimePicker)p.Controls[ctl.Name];
SkinDateTimePicker skdate_end = (SkinDateTimePicker)p.Controls[ctl.Name + "t"];
if (skdate_sta.text != "")
{
if (skdate_end.text == "")
{
condition = skdate_sta.Name + "='" + skdate_sta.text + "'";
}
else
{
condition = skdate_sta.Name + "='" + skdate_sta.text + "' and " + skdate_sta.Name + "='" + skdate_end.text + "'";
}
}
}
#endregion
}
master_condition += (condition == "") ? "" : "(" + condition + ") and ";
#endregion //遍历panel查找条件
}
string condition_sql = (master_condition == "") ? "" : master_condition.Substring(0, master_condition.Length - 5);
return condition_sql;
}
}
工具用一个窗体代替:
public partial class condition : Form
{
public condition()
{
InitializeComponent();
}
public event EventHandler subm;
public event EventHandler cleari;
private void submit_Click(object sender, EventArgs e)
{
subm(sender, e);
this.Dispose();
}
private void clearit_Click(object sender, EventArgs e)
{
this.Dispose();
dv.Rows.Clear();
cleari(sender, e);
}
}
当我们执行多条件的时候就等于拼接条件
该类问题可以采用以下两种方法实现:
使用动态生成sql语句进行实现,根据不同查询条件控制生成不同的查询SQL语句,也就是where 后面的内容;
利用开发系统控制where条件,此方法最佳,不但可以控制单多查询条件,也可以实现复杂多条件的查询,例如 = ,,,like等复杂查询,亦可对排序需求进行控制;
以上为大概解决方法,如有疑问 请追问~~ 谢谢
+
deng d
de
`-
declare @str varchar(1000)='';
declare @i int =0;
set @str='select * from master.dbo.spt_values where 1=1'
if @i=0
set @str=@str+' and [type]=''p'''
Exec (@str)
给你思路,具体你自己去写:
1.你这个是有二类条件:必选的:前二个2选1;
后面三个条件是可选的,选全部等于没选就没必要加这个条件,关键字没填的话等同;
所以你其实就是1-4个条件。
2.实现建议用存储过程,参数根据实际情况传递,条件越多查询越慢,越慢(耗cpu和io)的条件要放在语句的最后。