sql - 数字=字符变化:基于 postgres 数据库的操作符不存在
问题描述
我有这样的查询:
Select office_name, ofc_code
from table1
where ofc_code in (select ofc_institution from table2 where id ='2')
如果我运行它,我会得到一个错误:
Operator does not exist:numeric = character varying
Hint : no operator matches the given name and argument types
在上面的查询中:ofc_code
是数字并且ofc_institution
是一个不同的字符
解决方案
不要将苹果与橙子进行比较。'2'
是一个字符串值,2
是一个数字。
ofc_institution
如果您确定它只包含数字,您还需要转换为数字(那么问题是:到底为什么要将数字存储在varchar
列中?):
Select office_name, ofc_code
from table1
where ofc_code in (select ofc_institution::numeric
from table2
where id = 2);
如果您不能确定它ofc_institution
始终是一个数字,那么将其ofc_code
转换为一个字符串 - 但这会引出一个问题,您为什么要比较这些列的开头:
Select office_name, ofc_code
from table1
where ofc_code::varchar in (select ofc_institution
from table2
where id = 2);
推荐阅读
- mongodb - 在我刷新数据库连接之前,Mongoose 不会获取数据
- graphql - 是否使用 GraphQL 查询或变异来发送外部消息
- autohotkey - 我的脚本在某些时候崩溃了,我已经消除了调试器能够发现的所有语法错误
- reactjs - 使用 React 在 ASP.NET Core 3 中更改用于获取数据的基本 URL
- javascript - 带有颜色和行号的 console.log 包装器
- wpf - .net Core 3.1 WPF 关闭 Cleartype?
- javascript - 如何将文本放入 react-icons 组件中?
- html - 更改侧面和卡片之间的边距大小
- macos - macOs Time Machine:两个独立的驱动器还是一个 RAID-1?
- python - Python,将函数应用于数据是参数的熊猫数据框