VBA中长的查询语句如何分行或者这条语句该如何写更好

来源:百度知道 编辑:UC知道 时间:2024/05/18 14:24:59
sql="SELECT child.儿童姓名, child.儿童性别, child.出生日期, child.户籍属性, child.在册情况, child.父亲姓名, child.母亲姓名, child.家庭电话, child.通讯地址 WHERE (child.出生日期>='2000-01-01')
AND (child.户籍属性<>'1') AND (child.通讯地址 Like '%木渎%') AND (child.在册情况 Not Like '%S%' And child.在册情况 Not Like '%J%' And child.在册情况 Not Like '%K%' And child.在册情况 Not Like '%L%' And child.在册情况 Not Like '%M%' And child.在册情况 Not Like '%N%' And child.在册情况 Not Like '%W%' And child.在册情况 Not Like '%S%') OR (child.出生日期>='2000-01-01') AND (child.户籍属性<>'1') AND (child.通讯地址 Like '%金桥%') AND (child.在册情况 Not Like '%S%' And child.在册情况 Not Like '%J%' And child.在册情况 Not Like '%K%' And child.在册情况 Not Like '%L%' And child.在册情况 Not Like '%M%' And child.在册情况 Not Like '%N%' And child.在册情况 Not Like '%W%' And child.在册情况 Not Like '%S%') OR (child.出生日期>='200

很佩服你的耐心,我看着都晕,伸把手吧!

如果“child.在册情况”的内容为一个字符可以把一组not like 合并成:

instr('SJKLMNW',child.在册情况)=0

即:“child.在册情况”中不包含S,J,K,L,M,N的(不知为什么你用了两次'%S%'?)

另外:好像有三部分(出生日期,户籍属性,在册情况)是一样的应该提到外面,像这样的结构:A*B*C*(x+y+z+...)

即类似下面(未试验,自己试吧)

(child.出生日期>='2000-01-01') AND (child.户籍属性<>'1') and instr('SJKLMNW',child.在册情况)=0 and (child.通讯地址 Like '%木渎%' OR child.通讯地址 Like '%金桥%' OR ....)

用CHARINDEX函数代替instr,用法:

CHARINDEX ( expression1 , expression2 [ , start_location ] )