我使用PostgreSQL 10并且我CREATE EXTENSION unaccent;成功运行.我有一个包含以下内容的plgsql函数
whereText := 'lower(unaccent(place.name)) LIKE lower(unaccent($1))';
之后,根据用户选择的内容,可以添加更多条款whereText.
在whereText最终使用的查询:
placewithkeys := '%'||placename||'%';
RETURN QUERY EXECUTE format('SELECT id, name FROM '||fromText||' WHERE '||whereText)
USING placewithkeys , event, date;
Run Code Online (Sandbox Code Playgroud)
将whereText := 'LOWER(unaccent(place.name)) LIKE LOWER(unaccent($1))';不起作用,即使我删除LOWER的部分.
我做的select __my_function('???');,我什么也没有回来,即使我应该回来的结果,因为在数据库中存在名称?????
如果我删除unaccent,并留下了LOWER它的工作原理,但没有口音:??带来了?????回来,因为它应该.这似乎unaccent是导致问题.
我错过了什么?我怎样才能解决这个问题?
由于有关于语法和可能的SQLi的评论,我提供了整个函数定义,现在更改为在希腊语中对重音不敏感和不区分大小写的工作:
CREATE FUNCTION __a_search_place
(placename text, eventtype integer, eventdate integer, eventcentury integer, constructiondate integer, constructioncentury integer, arstyle integer, artype …Run Code Online (Sandbox Code Playgroud) 这是我的表格(简化的,只有重要的列):
CREATE TABLE things (
id serial primary key
, name varchar
, blueprint json default '{}'
);
Run Code Online (Sandbox Code Playgroud)
以及一些示例数据:
# select * from things;
id | name | blueprint
----+---------+-----------------------------------------------------------------------------
1 | Thing 1 | {}
2 | Thing 2 | {"1":{"name":"Iskapola","wight":"2"}}
3 | Thing 3 | {"1":{"name":"Azamund","weight":"3"}, "2":{"name":"Iskapola","weight":"1"}}
4 | Thing 4 | {"1":{"name":"Ulamir","weight":"1"}, "2":{"name":"Azamund","weight":"1"}}
Run Code Online (Sandbox Code Playgroud)
我想选择键'Azamund'下任意位置的行name。像这样的东西:
# select * from things where * ->> 'name' = 'Azamund';
id | blueprint
----+----------------------------------------------------------------------------
7 | {"1":{"name":"Azamund","weight":"3"}, "2":{"name":"Iskapola","weight":"1"}} …Run Code Online (Sandbox Code Playgroud) 我正在寻找一种在数组中使用“ IN”子句查询postgres jsonb字段的方法。
假设我有一张桌子
CREATE TABLE test(
id uuid,
test_content jsonb,
PRIMARY KEY(id)
);
INSERT INTO test (id, test_content) VALUES
('aa82a8b8-33ef-4937-bd8c-8a4b40960f18', '[{"label":"a","label1":"1"},{"label":"b","label1":"2"}]'),
('ba82a8b8-33ef-4937-bd8c-8a4b40960f18', '[{"label":"c","label1":"3"}]'),
('da82a8b8-33ef-4937-bd8c-8a4b40960f18', '[{"label":"d","label1":"4"}]');
Run Code Online (Sandbox Code Playgroud)
我需要选择test_content数组中的label可能为b或的行d。
我试过了
SELECT *
FROM test
WHERE test_content @> '[{"label":"b"}]' OR test_content @> '[{"label":"d"}]'
Run Code Online (Sandbox Code Playgroud)
但是当我想用label1包含扩展查询时,2还是3变得复杂...
我需要的是 WHERE label IN ('b','d') AND label1 IN ('2','3')
jsonb运算符可以吗?
我有一个posts带有类型列的Postgres 表jsonb基本上是一个扁平的标签数组。
我需要做的是以某种方式运行一个 LIKE 查询 tags列元素以便我可以找到具有以部分字符串开头的标签的帖子。
这样的事情在 Postgres 中可能吗?我一直在寻找超级复杂的例子,但没有人描述过这种基本和简单的场景。
我当前的代码可以很好地检查是否有具有特定标签的帖子:
select * from posts where tags @> '"TAG"'
Run Code Online (Sandbox Code Playgroud)
我正在寻找一种在以下方面运行某些东西的方法
select * from posts where tags @> '"%TAG%"'
Run Code Online (Sandbox Code Playgroud) postgresql ×4
json ×2
jsonb ×2
arrays ×1
dynamic-sql ×1
nested ×1
plpgsql ×1
sql ×1
unaccent ×1