C#连接mysql数据库如何实现多条件查询
给你一个稍微复杂一点的查询,我设计的
创新互联专注于东莞企业网站建设,响应式网站设计,成都商城网站开发。东莞网站建设公司,为东莞等地区提供建站服务。全流程定制网站,专业设计,全程项目跟踪,创新互联专业和态度为您提供的服务
看上图我把第一列标签后面的控件命名有规律,比如计划工厂后面文本框是"计划工厂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);
}
}
当我们执行多条件的时候就等于拼接条件
mysql if是多条件该怎么写
处理“我想查 IF中同时满足这两个条件的总数”
我的理解是:
SELECT COUNT(*) FROM tougao_record WHERE accept_company_id=100 AND channel_type=1 AND check_status=6
下面是if语句里面多个条件的使用。
IF语句的标准形式IF(expr1,expr2,expr3)
expr1可以是单个表达式也可以是多个表达式,且,或||,非!
上面的语句可以这样写
select COUNT(IF(channel_type=1 check_status=6),1,0) FROM tougao_record WHERE accept_company_id=100
但是就我的理解,mysql在统计count的时候,不管count括号里面的内容,只管是否为空,查询的结果不为空就计数。
我是处理下面的问题用到了,可以直接跑一下我给的sql语句。
SET @destval:='0.63';
SET @valforcomp='0.62';
SELECT
IF(@destval REGEXP '^[\-\+.]?([0-9.]+)$' @valforcomp REGEXP '^[\-\+.]?([0-9.]+)$',
IF(ABS(@destval-@valforcomp)0.02,1,0),NULL) result
上面的REGEXP只是简单的用来判断是否是数字,通过这个我还发现了另一个问题,
SELECT ABS(@destval-@valforcomp);
这个查询出来不是等于0.01而是0.010000000000000009
不知道有没有帮到你。
mysql中一个字段同时满足多个条件,(3个以上)
SELECT
t.file_id
FROM
app_polly_file_labels t
WHERE
t.style = '清新'
or t.style = '甜美'
or t.style = '韩潮来袭'
GROUP BY t.file_id
HAVING count(t.file_id)=3
或者
SELECT
a.file_id
FROM
app_polly_file_labels a
INNER JOIN app_polly_file_labels b ON a.file_id = b.file_id
INNER JOIN app_polly_file_labels c ON a.file_id = c.file_id
WHERE
a.style ='清新'
AND b.style ='甜美'
AND c.style ='韩潮来袭'
扩展资料:
sql常用的复杂查询语句
一、选择指定的列
select Sno as 学号,Sname as 姓名 from student
select Sno,Sname from student
二、查询全部列
select * from student
三、对查询后的指定列进行命名
select Sname as 姓名,(2014-Sage) as 出生年 from student
select Sname ,(2014-Sage) from student
四、消除取值重复的行
select distinct Sno as 选修了课程的学生学号 from SC
select distinct Sno from SC
五、选择表中若干元组,满足条件的
select Sname as 学生姓名 from student where Sdept='IS'
参考链接:SQL语句大全
网页标题:mysql怎么多条件 mysql多条件筛选
分享URL:http://lswzjz.com/article/doiecho.html