SELECT模糊查询
发布网友
发布时间:2022-04-09 09:50
我来回答
共2个回答
热心网友
时间:2022-04-09 11:19
一般模糊语句如下:
SELECT 字段 FROM 表 WHERE 某字段 Like 条件
其中关于条件,SQL提供了四种匹配模式:
1,%:表示任意0个或多个字符。可匹配任意类型和长度的字符,有些情况下若是中文,请使用两个百分号(%%)表示。
比如 SELECT * FROM [user] WHERE u_name LIKE '%三%'
将会把u_name为“张三”,“张猫三”、“三脚猫”,“唐三藏”等等有“三”的记录全找出来。
另外,如果需要找出u_name中既有“三”又有“猫”的记录,请使用and条件
SELECT * FROM [user] WHERE u_name LIKE '%三%' AND u_name LIKE '%猫%'
若使用 SELECT * FROM [user] WHERE u_name LIKE '%三%猫%'
虽然能搜索出“三脚猫”,但不能搜索出符合条件的“张猫三”。
2,_: 表示任意单个字符。匹配单个任意字符,它常用来*表达式的字符长度语句:
比如 SELECT * FROM [user] WHERE u_name LIKE '_三_'
只找出“唐三藏”这样u_name为三个字且中间一个字是“三”的;
再比如 SELECT * FROM [user] WHERE u_name LIKE '三__';
只找出“三脚猫”这样name为三个字且第一个字是“三”的;
3,[ ]:表示括号内所列字符中的一个(类似正则表达式)。指定一个字符、字符串或范围,要求所匹配对象为它们中的任一个。
比如 SELECT * FROM [user] WHERE u_name LIKE '[张李王]三'
将找出“张三”、“李三”、“王三”(而不是“张李王三”);
如 [ ] 内有一系列字符(01234、abcde之类的)则可略写为“0-4”、“a-e”
SELECT * FROM [user] WHERE u_name LIKE '老[1-9]'
将找出“老1”、“老2”、……、“老9”;
4,[^ ] :表示不在括号所列之内的单个字符。其取值和 [] 相同,但它要求所匹配对象为指定字符以外的任一个字符。
比如 SELECT * FROM [user] WHERE u_name LIKE '[^张李王]三'
将找出不姓“张”、“李”、“王”的“赵三”、“孙三”等;
SELECT * FROM [user] WHERE u_name LIKE '老[^1-4]';
将排除“老1”到“老4”,寻找“老5”、“老6”、……
5,查询内容包含通配符时
由于通配符的缘故,导致我们查询特殊字符“%”、“_”、“[”的语句无法正常实现,而把特殊字符用“[ ]”括起便可正常查询。据此我们写出以下函数:
function sqlencode(str)
str=replace(str,"[","[[]") '此句一定要在最前
str=replace(str,"_","[_]")
str=replace(str,"%","[%]")
sqlencode=str
end function
在查询前将待查字符串先经该函数处理即可。
热心网友
时间:2022-04-09 12:37
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new frmBookManage());
}
private void btnExit_Click(object sender, System.EventArgs e)
{
myCon.Close();//关闭连接
Application.Exit();//退出程序
}
private void frmBookManage_Load(object sender, System.EventArgs e)
{
//连接对象
myCon = new OleDbConnection();
myCon.ConnectionString = "Provider = Microsoft.Jet.OLEDB.4.0;"+"Data source ="+Application.StartupPath +@"\LibraryInfo.mdb";
myCon.Open();//打开数据库连接
OleDbCommand myCom = new OleDbCommand("select SortName from SortInfo",myCon);
myRd = myCom.ExecuteReader();
while(myRd.Read()==true)
{
cmbInfo.Items.Add(myRd["SortName"]);
}
myRd.Close();
}
private void btnView_Click(object sender, System.EventArgs e)
{
if(cmbInfo.Text=="" && txtBookName.Text=="" && txtAuthor.Text=="")
{
OleDbDataAdapter myAd = new OleDbDataAdapter("select BookId as 类别名称,BookName as 图书名称,BookAuthor as 作者,BookPublish as 出版社 from BookInfo ",myCon);
DataSet myDs = new DataSet();
myAd.Fill(myDs,"BookInfo");
dataGrid1.DataSource=myDs.Tables[0];
}
else
if(cmbInfo.Text=="" && txtBookName.Text=="")
{
OleDbDataAdapter myAd = new OleDbDataAdapter("select BookName as 图书名称,BookAuthor as 作者,BookPublish as 出版社 from BookInfo where BookAuthor='"+txtAuthor.Text+"' ",myCon);
DataSet myDs = new DataSet();
myAd.Fill(myDs,"BookInfo");
dataGrid1.DataSource=myDs.Tables[0];
}
else
if(cmbInfo.Text=="" && txtAuthor.Text=="")
{
OleDbDataAdapter myAd = new OleDbDataAdapter("select BookName as 图书名称,BookAuthor as 作者,BookPublish as 出版社 from BookInfo where BookName='"+txtBookName.Text+"'",myCon);
DataSet myDs = new DataSet();
myAd.Fill(myDs,"BookInfo");
dataGrid1.DataSource=myDs.Tables[0];
}
else
if(cmbInfo.Text=="")
{
OleDbDataAdapter myAd = new OleDbDataAdapter("select BookName as 图书名称,BookAuthor as 作者,BookPublish as 出版社 from BookInfo where BookName='"+txtBookName.Text+"' and BookAuthor='"+txtAuthor.Text+"'",myCon);
DataSet myDs = new DataSet();
myAd.Fill(myDs,"BookInfo");
dataGrid1.DataSource=myDs.Tables[0];
}
else
if(cmbInfo.Text=="小说")
{
OleDbDataAdapter myAd = new OleDbDataAdapter("select BookName as 图书名称,BookAuthor as 作者,BookPublish as 出版社 from BookInfo where SortId=1",myCon);
DataSet myDs = new DataSet();
myAd.Fill(myDs,"BookInfo");
dataGrid1.DataSource=myDs.Tables[0];
}
else
if(cmbInfo.Text=="计算机")
{
OleDbDataAdapter myAd = new OleDbDataAdapter("select BookName as 图书名称,BookAuthor as 作者,BookPublish as 出版社 from BookInfo where SortId=2",myCon);
DataSet myDs = new DataSet();
myAd.Fill(myDs,"BookInfo");
dataGrid1.DataSource=myDs.Tables[0];
}
else
if(cmbInfo.Text=="求职")
{
OleDbDataAdapter myAd = new OleDbDataAdapter("select BookName as 图书名称,BookAuthor as 作者,BookPublish as 出版社 from BookInfo where SortId=3",myCon);
DataSet myDs = new DataSet();
myAd.Fill(myDs,"BookInfo");
dataGrid1.DataSource=myDs.Tables[0];
}
else
if(cmbInfo.Text=="科学")
{
OleDbDataAdapter myAd = new OleDbDataAdapter("select BookName as 图书名称,BookAuthor as 作者,BookPublish as 出版社 from BookInfo where SortId=4",myCon);
DataSet myDs = new DataSet();
myAd.Fill(myDs,"BookInfo");
dataGrid1.DataSource=myDs.Tables[0];
}
}