请教,这段SQL语句应该怎么写?
private void Search()
{
string ssql = "select distinct a.STAFF_ID,a.CNNAME,a.DEPT,a.TECHPOS,a.EDULEVEL,a.ATTACH_DATE,b.M_SKILL,b.M_TYPE,c.LANGUAGE ";
ssql += " from D_STAFF_BASE a,D_SKILL_VIEW b,D_STAFF_LANG_ABILITY c ";
//根据姓名查询
if (this.TextBox1.Text.Trim() != " ")
{
ssql += " where a.CNNAME= ' "+this.TextBox1.Text.Trim()+ " ' ";
}
//根据技能查询
if (this.TextBox3.Text.Trim() != " ")
{
ssql += " where b.M_SKILL= ' "+this.TextBox3.Text.Trim()+ " 'or b.M_TYPE= ' "+this.TextBox3.Text.Trim()+ " ' ";
}
//根据语言查询
if (this.Languageddl.SelectedValue!= " ")
{
ssql += " where c.LANGUAGE= ' "+this.Languageddl.DataValueField+ " ' ";
}
//根据职称查询
if (this.Techposddl.SelectedValue != " ")
{
ssql += " where a.TECHPOS= ' "+this.Techposddl.DataValueField+ " ' ";
}
if (this.Deptddl.SelectedValue != " ")
{
ssql += " where a.DEPT= ' "+this.Deptddl.DataValueField+ " ' ";
}
//GridView1.DataSourceID = "SqlDataSource1 ";
this.SqlDataSource1.SelectCommand = ssql;
this.DataBind();
}
根据不同的情况查询,我的这个SQL语句老是写不对,应该怎么写啊?
[解决办法]
改 ssql += " from D_STAFF_BASE a,D_SKILL_VIEW b,D_STAFF_LANG_ABILITY c where 1=1 ";
然后
//根据姓名查询
if (this.TextBox1.Text.Trim() != " ")
{
ssql += " and a.CNNAME= ' "+this.TextBox1.Text.Trim()+ " ' ";
}
后面的都将“Where”改为“and”