postgresql uuid模糊搜索 uuid string 类型转换 SQL Error [42883] explicit type casts
问题描述
在 postgresql 的使用过程中,你可能有以下几种需求:
要对 uuid 类型的列进行过滤,但是需要手动输入整个uuid太麻烦容易出错,如果 uuid 列也能像字符串一样模糊匹配就好了
对 uuid 列直接进行模糊匹配,会报错 SQL Error [42883]
有没有什么办法可以对uuid/字符串类型进行转换?
如果可以转换类型,postgresql 还有哪些其他的类型?
uuid 模糊搜索 变量/列值类型转换
要对 postgresql 中某张表进行过滤,过滤字段为id uuid,uuid不好手动输入啊,如果能对 uuid 类型的列进行模糊匹配就好了。
select * tbl_user where id = 'bf9d1fdb-d9fb-4a99-86cb-df46e2410a33'; -- 会报错 SQL Error [42883] select * tbl_user where id like 'bf9d1fdb-d9fb-4a99-86cb-df46e2410a33'; -- 将id列视为指定的某种类型-->character varying,然后实际上是对字符串进行模糊匹配 select * tbl_user where id::character varying like 'bf9d1fdb-d9fb-4a99-86cb-df46e2410a33'; -- 模糊匹配中可以使用 _ % 等匹配符号。 select * tbl_user where id::character varying like 'bf9d1fdb%';
SQL Error [42883]
org.jkiss.dbeaver.model.sql.DBSQLException: SQLException: SQL Error [42883]: ERROR: operator does not exist: uuid -- unknow Hint: No operator matches the given name and argument types. You might need to add explicit type casts.
形如::数据类型的写法,可以对变量/列/值进行类型转换。即为explicit type casts 明确的类型转换。
Quiz
select 'ddff338c-4ee2-41e7-9216-6ca9dbd03738', 'ddff338c-4ee2-41e7-9216-6ca9dbd03738'::uuid, 'ddff338c-4ee2-41e7-9216-6ca9dbd03738'::character varying ;
以上SQL会查出3列1行的结果集。问题是:每列的数据类型分别是什么?
字符串
UUID
字符串
更多类型
你想不想知道像这样 ::数据类型的写法,还有哪些类型名称可以写?
见 https://www.postgresql.org/docs/current/datatype.html
来源:https://blog.csdn.net/wuyujin1997/article/details/125772258
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)