![]() ![]() |
|
*和%到底有何分别 | |
作者:佚名 文章来源:不详 点击数 更新时间:2008/5/24 20:05:13 文章录入:杜斌 责任编辑:杜斌 | |
|
|
问题: LIKE 语句到底如何组织?*和%该用哪个? 用like 不行,如何进行糊模查询? 回答: 在回答上述问题时你必须弄清楚你的环境 1、在纯 access 环境中,并且没有开启 ANSI SQL 兼容选项的情况下: 在这种环境下仍然至少要分成3种情况 1.1、在 VBA 代码中组织 JET SQL 语句: dim rs as new adodb.recordset dim strSQL as string dim 变量 as string strSQL="select * from table where field like '*" & 变量 & "*'" '如果是 ANSI SQL 兼容模式时,必须用 'strSQL="select * from table where field like '%" & 变量 & "%'" '代替 '如果不使用变量可以直接这样组织 'strSQL="select * from table where field like '%字符串%'" 'strSQL="select * from table where field like '*字符串*'" rs.open strsql ,adodb.connection,1,1 1.2、直接在新建查询中写 SQL 代码,并保存为一个查询备用 select * from table where field like '*' & forms!某个窗体名!控件名 & '*' 千万注意,不能写成以下形式,以下形式是错误的: select * from table where field like '*forms!某个窗体名!控件名*' 1.3、在窗体的 RECORDSOURCE 数据源属性或者控件的 ROWSOURCE行来源属性中 在这种情况中,同1.2是相同的。 2、在纯 access 环境中,并且已经开启 ANSI SQL 兼容选项的情况下: 在这种情况下也一样至少要分3种情况 2.1、在 VBA 代码中组织 JET SQL 语句: dim rs as new adodb.recordset dim strSQL as string dim 变量 as string strSQL="select * from table where field like '%" & 变量 & "%'" '如果是非 ANSI SQL 兼容模式时,必须用 'strSQL="select * from table where field like '*" & 变量 & "*'" '代替 '如果不使用变量可以直接这样组织 'strSQL="select * from table where field like '%字符串%'" 'strSQL="select * from table where field like '*字符串*'" rs.open strsql ,adodb.connection,1,1 2.2、直接在新建查询中写 SQL 代码,并保存为一个查询备用 select * from table where field like '%' & forms!某个窗体名!控件名 & '%' 千万注意,不能写成以下形式,以下形式是错误的: select * from table where field like '%forms!某个窗体名!控件名%' 2.3、在窗体的 RECORDSOURCE 数据源属性或者控件的 ROWSOURCE行来源属性中 在这种情况中,同2.2是相同的。 3、在非 access 环境中,只是 VB ASP DELPHI 等调用 MDB 格式的文件的情况下: 以下以 VB 举例 dim strSQL as string strSQL="select * from table where field like '%" & 某字符串变量名 & "%'" |
|
![]() ![]() |