"LIKE"无法按预期工作

Pet*_*Mmm 2 postgresql sql-like

在Postgresql 8中为什么这样可以

select * from prod where code like '1%'
select * from prod where code like '%1'
Run Code Online (Sandbox Code Playgroud)

但这会返回0行(有代码以数字1开头/结尾)

select * from prod where code like '1%1'
Run Code Online (Sandbox Code Playgroud)

更新

这发生在我目前的安装中:

# psql --version
psql (PostgreSQL) 8.3.7


create table a(code char(10));
CREATE TABLE
db=# insert into a values('111');
INSERT 0 1
db=# select * from a where code like '1%';
    code
------------
 111
(1 row)

db=# select * from a where code like '%1';
 code
------
(0 rows)

db=# select * from a where code like '1%1';
 code
------
(0 rows)
Run Code Online (Sandbox Code Playgroud)

更新2

这是数据类型!用varchar就可以了!

谢谢.

小智 7

是因为数据类型是char(10)吗?

这意味着即使你只是插入一些像"111"这样的东西,它总会占用10个字符.因此,如果最后不使用带有"1"的10个字符的字符串,则"%1"和"1%1"将永远不会匹配.