gpt4 book ai didi

postgresql - "LIKE"没有按预期工作

转载 作者:行者123 更新时间:2023-11-29 11:53:58 24 4
gpt4 key购买 nike

在 Postgresql 8 中为什么这样可以

select * from prod where code like '1%'
select * from prod where code like '%1'

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

select * from prod where code like '1%1'

更新

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

# 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)

更新 2

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

谢谢。

最佳答案

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

这意味着它总是占用 10 个字符,即使您只是插入更短的字符,如“111”。因此,如果您不使用结尾为“1”的 10 个字符的字符串,则“%1”和“1%1”永远不会匹配。

关于postgresql - "LIKE"没有按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1543133/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com