我如何在 postgreSQL 中将 bigint 转换为 int

-6 postgresql

我想在 bigint 类型的列之一中找出负值。所以当我选择 *from tablename where columname(bigint) <0 ...它给我一个错误。

SQL Error [42883]: ERROR: operator does not exist: bigint[] < integer
  Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts.
  Position: 60
  ERROR: operator does not exist: bigint[] < integer
  Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts.
  Position: 60
Run Code Online (Sandbox Code Playgroud)

表定义为:

CREATE TABLE oper.asset_input_event 
(
  id                   bigserial NOT NULL,
  trip_ids             bigint[],
  cassandra_uuid       uuid,
  asset_id             integer,
  zbox_input_id        integer,
  org_gps_id           integer,
  gpssn                bigint,
  input_state          boolean,
  interrupt_code       characteR VARYING(25),
  start_geography_id   integer,
  end_geography_id     integer,
  is_cold_start        boolean,
);
Run Code Online (Sandbox Code Playgroud)

查询是:

SELECT trip_ids 
from oper.asset_input_event 
where trip_ids < 0
Run Code Online (Sandbox Code Playgroud)

a_h*_*ame 5

您的列不是 bigint 类型,它是一个整数数组

如果您仔细阅读错误消息,它会准确地告诉您:

运算符不存在:bigint[] < integer

它表示该<运算符未定义为将bigint数组与单个整数值进行比较。

我只能猜测你想做什么。我假设您正在寻找存储在该数组中的至少一个id 小于 0 的行。

当涉及数组时,您需要使用allorany运算符与数组中的所有值进行比较。不幸的是你不能写,any(trip_ids) < 0所以你需要将条件反转为:

SELECT trip_ids 
from oper.asset_input_event 
where 0 > all(trip_ids);
Run Code Online (Sandbox Code Playgroud)

这将返回trip_ids数组中所有值都小于 0 的所有行。如果你想找到那些至少一个小于 0 的行,你可以使用any运算符。

SELECT trip_ids 
from oper.asset_input_event 
where 0 > any(trip_ids);
Run Code Online (Sandbox Code Playgroud)

使用数组通常表示错误的数据模型。这应该更好地标准化为适当的 1:many 关系。