postgre integer 模糊查询

postgresql中integer字段的模糊查询。数据库中string类型的模糊查询很简单,integer类型的字段模糊查询没有简便方法。

postgresql可以使用如下方法:

?
1
2
3
select * from task_requirement
where 1=1
and to_char(version, '999999999' ) like '%' ||ltrim(to_char(1, '9' ))|| '%'

将version字段变为字符串类型to_char(version,'999999999'),999999999很大了,相信版本号不需要这么大。

将参数也用to_char转化为字符串类型;其中1是传的参数,而‘9’是位数,to_char的用法,将1转换为‘1’,若是to_char(1,'99'),则将1转换成‘01’,缺位数则用0补全,

使用replace方法将0去除,存在问题,模糊查询0的version时存在问题。所以用java定义一个新属性versionLen,处理然后将长度传过来。

?
1
2
3
4
5
6
7
8
9
10
11
Integer version = taskRequirement.getVersion();
Integer versionLen = 0;
while(version>=0){
     if(version==0){
         versionLen = 1;
         break;
     } else {
         version /= 10;
         versionLen++;
     }
}
?
1
2
3
<if test= "record != null and record.version != null and record.versionLen != null" >
     and to_char(version, '999999999' ) like '%' ||ltrim(to_char(${record.version}, '' ||${record.versionLen}))|| '%'
</if>

还有一点就是只能使用‘9’这样的带能将数值转为字符串;在使用to_char(1,'9')的时候,前面会出现一个空格不知道为什么,所以用ltrim去除左边空格,就能进行inteher类型字段模糊查询了。

其实他的代码有的地方我没看懂例如version /= 10;还有to_char(1,'9')的时候下面写的是''||${record.versionLen}))

我直接加的9999999就好使了,没有自动补0


猜你喜欢

转载自blog.csdn.net/qq_38623459/article/details/79989679