我需要一种方法来测试给定数组中是否存在值.到目前为止,我想出了类似的东西
select '{1,2,3}'::int[] @> (ARRAY[]::int[] || value_variable::int)
Run Code Online (Sandbox Code Playgroud)
但我一直认为应该有一个更简单的方法,我只是看不到它.
编辑:刚认识到我能做到这一点
select '{1,2,3}'::int[] @> ARRAY[value_variable::int]
Run Code Online (Sandbox Code Playgroud)
这要好得多,我相信这已经足够了,但如果你有其他方法可以做,请分享.
我有两个postgresql表:
table name column names
----------- ------------------------
login_log ip | etc.
ip_location ip | location | hostname | etc.
Run Code Online (Sandbox Code Playgroud)
我想得到每个login_log
没有行的IP地址ip_location
.
我尝试了这个查询,但它抛出了语法错误.
SELECT login_log.ip
FROM login_log
WHERE NOT EXIST (SELECT ip_location.ip
FROM ip_location
WHERE login_log.ip = ip_location.ip)
Run Code Online (Sandbox Code Playgroud)
Run Code Online (Sandbox Code Playgroud)ERROR: syntax error at or near "SELECT" LINE 3: WHERE NOT EXIST (SELECT ip_location.ip`
我也想知道这个查询(通过调整使其工作)是否是用于此目的的最佳性能查询.
PostgreSQL中IN
和ANY
运算符有什么区别?
两者的工作机制似乎是一样的.任何人都能用一个例子来解释这个吗
这是我的函数声明和正文的一部分:
CREATE OR REPLACE FUNCTION access_update()
RETURNS void AS $$
DECLARE team_ids bigint[];
BEGIN
SELECT INTO team_ids "team_id" FROM "tmp_team_list";
UPDATE "team_prsnl"
SET "updt_dt_tm" = NOW(), "last_access_dt_tm" = NOW()
WHERE "team_id" IN team_ids;
END; $$ LANGUAGE plpgsql;
Run Code Online (Sandbox Code Playgroud)
我想team_ids
成为一个我可以在UPDATE
声明中使用的一组int .这个函数给我这样的错误:
psql:functions.sql:62: ERROR: syntax error at or near "team_ids"
LINE 13: AND "team_id" IN team_ids;
Run Code Online (Sandbox Code Playgroud) 我正在尝试获取类型列text[]
包含类似于某些用户输入的值的行。
到目前为止,我一直在想和做的是使用'ANY'
and 'LIKE
'运算符,如下所示:
select * from someTable where '%someInput%' LIKE ANY(someColum);
Run Code Online (Sandbox Code Playgroud)
但这是行不通的。该查询返回与此查询相同的值:
select * from someTable where 'someInput' = ANY(someColum);
Run Code Online (Sandbox Code Playgroud)
使用unnest()
子查询中的函数我得到了很好的结果,但是WHERE
如果可能,我需要查询此in 子句。
为什么LIKE
操作员不与操作员一起工作,ANY
并且我没有出现任何错误?我认为原因之一应该是ANY
运算符在查询的右边,但是...
unnest()
如果不使用WHERE
条款,是否有解决方案?
我JSONB
在PostgreSQL中有一个对象:
'{"cars": ["bmw", "mercedes", "pinto"], "user_name": "ed"}'
Run Code Online (Sandbox Code Playgroud)
我试图在a的子句中使用其中"cars"数组的值:WHERE
SELECT
SELECT car_id FROM cars WHERE car_type IN ('bmw', 'mercedes', 'pinto');
Run Code Online (Sandbox Code Playgroud)
这将正确返回值1,2和3 - 请参阅本文底部的表格设置.
目前,在我的功能中我这样做:
(1) Extract the "cars" array into a variable `v_car_results`.
(2) Use that variable in the `WHERE` clause.
Pseudo code:
DECLARE v_car_results TEXT
BEGIN
v_car_results = '{"cars": ["bmw", "mercedes", "pinto"], "user_name": "ed"}'::json#>>'{cars}';
-- this returns 'bmw', 'mercedes', 'pinto'
SELECT car_id FROM cars WHERE car_type IN ( v_car_results );
END
Run Code Online (Sandbox Code Playgroud)
但是,该SELECT
语句不返回任何行.我知道它正在阅读这3种车型作为单一 …
我有自定义类型
CREATE TYPE mytype as (id uuid, amount numeric(13,4));
Run Code Online (Sandbox Code Playgroud)
我想将它传递给具有以下签名的函数:
CREATE FUNCTION myschema.myfunction(id uuid, mytypes mytype[])
RETURNS BOOLEAN AS...
Run Code Online (Sandbox Code Playgroud)
如何在postgres查询中调用它,并且不可避免地从PHP中调用?
我正在努力解决看似红宝石的语义问题.我正在编写一个从表单中获取可变数量的参数并创建Postgresql查询的方法.
def self.search(params)
counter = 0
query = ""
params.each do |key,value|
if key =~ /^field[0-9]+$/
query << "name LIKE ? OR "
counter += 1
end
end
query = query[0..-4] #remove extra OR and spacing from last
params_list = []
(1..counter).each do |i|
field = ""
field << '"%#{params[:field'
field << i.to_s
field << ']}%", '
params_list << field
end
last_item = params_list[-1]
last_item = last_item[0..-3] #remove trailing comma and spacing
params_list[-1] = last_item
if params …
Run Code Online (Sandbox Code Playgroud) 我正在使用 Postgres 9.2.24。
我有一个以_order
大约 100,000,000 行命名的表。该表有一个名为 的列merged_id int8
。大约 2,000,000_order
行有一个merged_id
值,其余的为空。
我找到了两种不同的 Postgres 行为,我在其中_order
使用查询进行搜索
select * from _order where merged_id in ( 10001 ,10002 ,10003 ....., 11000);
Run Code Online (Sandbox Code Playgroud)
如果我创建这样的索引:
create index order_merged_id_index on _order(merged_id);
Run Code Online (Sandbox Code Playgroud)
无论 in 子句中有多少个 id(测试从 1 到 50 到 100 到 200 到 1000)EXPLAIN
显示搜索都将使用index_scan
.
但是如果我创建这个部分索引:
create index order_merged_id_index on _order(merged_id) where merged_id is not null;
Run Code Online (Sandbox Code Playgroud)
EXPLAIN
seq_scan
在WHERE
子句中显示100 多个 ID 号。
为什么是这样?
有什么办法可以解决吗?
我使用go和gorm编写了一个API,它在我们的数据库上运行计算并返回结果.
我只是IN
在使用聚合时达到了条件的参数限制.示例查询:
SELECT SUM(total_amount) from Table where user_id in(...70k parameters) group by user_id
Run Code Online (Sandbox Code Playgroud)
我当前的一个边缘案例有> 65535个用户ID,所以我的Postgres客户端抛出一个错误:
Run Code Online (Sandbox Code Playgroud)got 66037 parameters but PostgreSQL only supports 65535 parameters
我不确定最好的办法是什么.一个将处理此边缘情况的大量参数,同时不影响我的典型用例.我是否对id进行分块并迭代多次查询将其存储在内存中,直到我拥有所需的所有数据?用ANY(VALUES)
...
显然,从查询中我对Postgres的知识非常有限,所以任何帮助都会令人难以置信地受到赞赏.
我的类型为:
CREATE TYPE status_record AS
(
id bigint,
status boolean
);
Run Code Online (Sandbox Code Playgroud)
使用类型数组作为输入参数进行某些处理的过程,如下所示:
CREATE OR REPLACE FUNCTION update_status(status_list status_record[])
RETURNS text AS
$BODY$
DECLARE
BEGIN
--does some processing
return 'SUCCESS';
end;$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
Run Code Online (Sandbox Code Playgroud)
最后我查询程序为:
select *
from update_status(cast(ARRAY[(385,false),(387,false)] as status_record[]));
Run Code Online (Sandbox Code Playgroud)
在 pgadmin 中一切正常。后来,当我尝试使用Hibernate 本机 SQL 查询调用相同的命令时,Ka Boom!!!显示如下:
Run Code Online (Sandbox Code Playgroud)org.postgresql.util.PSQLException: ERROR: array value must start with "{" or dimension information
最后一个问题:两者ARRAY[--something]
的{--something}
作用相同吗?