使用postgresql 9.3我可以选择JSON数据类型的特定字段,但是如何使用UPDATE修改它们?我在postgresql文档中或在线任何地方都找不到任何这样的例子.我试过了明显的事:
postgres=# create table test (data json);
CREATE TABLE
postgres=# insert into test (data) values ('{"a":1,"b":2}');
INSERT 0 1
postgres=# select data->'a' from test where data->>'b' = '2';
?column?
----------
1
(1 row)
postgres=# update test set data->'a' = to_json(5) where data->>'b' = '2';
ERROR: syntax error at or near "->"
LINE 1: update test set data->'a' = to_json(5) where data->>'b' = '2...
Run Code Online (Sandbox Code Playgroud) 我需要从json类型列中删除一些属性.
桌子:
CREATE TABLE my_table( id VARCHAR(80), data json);
INSERT INTO my_table (id, data) VALUES (
'A',
'{"attrA":1,"attrB":true,"attrC":["a", "b", "c"]}'
);
Run Code Online (Sandbox Code Playgroud)
现在,我需要attrB从列中删除data.
像alter table my_table drop column data->'attrB';这样的东西会很好.但是使用临时表的方法也足够了.
试图在带有rails的模式上做区别.
2.1.1 :450 > u.profiles.select("profiles.*").distinct
Profile Load (0.9ms) SELECT DISTINCT profiles.* FROM "profiles" INNER JOIN "integration_profiles" ON "profiles"."id" = "integration_profiles"."profile_id" INNER JOIN "integrations" ON "integration_profiles"."integration_id" = "integrations"."id" WHERE "integrations"."user_id" = $1 [["user_id", 2]]
PG::UndefinedFunction: ERROR: could not identify an equality operator for type json
LINE 1: SELECT DISTINCT profiles.* FROM "profiles" INNER JOIN "integ...
^
: SELECT DISTINCT profiles.* FROM "profiles" INNER JOIN "integration_profiles" ON "profiles"."id" = "integration_profiles"."profile_id" INNER JOIN "integrations" ON "integration_profiles"."integration_id" = "integrations"."id" WHERE "integrations"."user_id" = $1
ActiveRecord::StatementInvalid: …Run Code Online (Sandbox Code Playgroud) 我在PostgreSQL中找不到有关JSON模式验证的任何信息,有没有办法在PostgreSQL JSON数据类型上实现JSON模式验证?
在PostgreSQL 9.3中,有多种方法可以构建一个表达式,它指向一个json字段的嵌套属性:
data->'foo'->>'bar'
data#>>'{foo,bar}'
json_extract_path_text(data, 'foo', 'bar')
Run Code Online (Sandbox Code Playgroud)
因此,如果查询的表达式与索引的表达式完全匹配,则PostgreSQL仅使用这些索引.
CREATE TABLE json_test_index1(data json);
CREATE TABLE json_test_index2(data json);
CREATE TABLE json_test_index3(data json);
CREATE INDEX ON json_test_index1((data->'foo'->>'bar'));
CREATE INDEX ON json_test_index2((data#>>'{foo,bar}'));
CREATE INDEX ON json_test_index3((json_extract_path_text(data, 'foo', 'bar')));
-- these queries use an index, while all other combinations not:
EXPLAIN SELECT * FROM json_test_index1 WHERE data->'foo'->>'bar' = 'baz';
EXPLAIN SELECT * FROM json_test_index2 WHERE data#>>'{foo,bar}' = 'baz';
EXPLAIN SELECT * FROM json_test_index3 WHERE json_extract_path_text(data, 'foo', 'bar') = 'baz';
Run Code Online (Sandbox Code Playgroud)
我的问题是:
这种行为是有意的吗?我认为查询优化器应该(至少)使用带有#>>运算符的索引,当查询包含适当的调用时json_extract_path_text()- …
我在postgres 9.3.5中有一个表格,如下所示:
CREATE TABLE customer_area_node
(
id bigserial NOT NULL,
customer_id integer NOT NULL,
parent_id bigint,
name text,
description text,
CONSTRAINT customer_area_node_pkey PRIMARY KEY (id)
)
Run Code Online (Sandbox Code Playgroud)
我查询:
WITH RECURSIVE c AS (
SELECT *, 0 as level, name as path FROM customer_area_node WHERE customer_id = 2 and parent_id is null
UNION ALL
SELECT customer_area_node.*,
c.level + 1 as level,
c.path || '/' || customer_area_node.name as path
FROM customer_area_node
join c ON customer_area_node.parent_id = c.id
)
SELECT * FROM c ORDER …Run Code Online (Sandbox Code Playgroud) 我为我的问题准备了一个SQL Fiddle -
给出下表:
CREATE TABLE chat(
gid integer, /* game id */
uid integer, /* user id */
created timestamptz,
msg text
);
Run Code Online (Sandbox Code Playgroud)
填充以下测试数据:
INSERT INTO chat(gid, uid, created, msg) VALUES
(10, 1, NOW() + interval '1 min', 'msg 1'),
(10, 2, NOW() + interval '2 min', 'msg 2'),
(10, 1, NOW() + interval '3 min', 'msg 3'),
(10, 2, NOW() + interval '4 min', 'msg 4'),
(10, 1, NOW() + interval '5 min', 'msg 5'),
(10, 2, …Run Code Online (Sandbox Code Playgroud) 为了在 postgresql 中保存 json 格式的列表,我com.vladmihalcea:hibernate-types-52在 Spring boot 2 中使用如下:
@Entity
@TypeDefs({
@TypeDef(name = "jsonb", typeClass = JsonBinaryType.class)
})
public class Profile {
...
@Type(type = "jsonb")
@Column(columnDefinition = "jsonb")
private List<String> locations;
// getters & setters
}
Run Code Online (Sandbox Code Playgroud)
有了这个,一切都OK了,我可以将位置保存为postgresql中的json。但我想将位置保存为类继承结构。课程如下:
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = As.PROPERTY, property = "type")
@JsonSubTypes({
@JsonSubTypes.Type(value = SubClass1.class, name = "subClass1"),
@JsonSubTypes.Type(value = SubClass2.class, name = "subClass2")
})
public abstract class MyClass {
private String name;
// getters & setters
}
public class SubClass1 extends …Run Code Online (Sandbox Code Playgroud) 这是 jsonb 列的虚拟数据
[ { "name": [ "sun11", "sun12" ], "alignment": "center", "more": "fields" }, { "name": [ "sun12", "sun13" ], "alignment": "center" }, { "name": [ "sun14", "sun15" ] }]
Run Code Online (Sandbox Code Playgroud)
我想从 jsonb 对象数组中获取所有名称键值...期待输出 -
[ [ "sun11", "sun12" ], [ "sun12", "sun13" ], [ "sun14", "sun15" ] ]
Run Code Online (Sandbox Code Playgroud)
问题是我可以通过提供 0、1 等索引来获取名称键值
SELECT data->0->'name' FROM public."user";
[ "sun11", "sun12" ]
Run Code Online (Sandbox Code Playgroud)
但是我无法从同一个对象数组中获取所有名称键值。我只想从 json 对象数组中获取所有键值。任何帮助都会有所帮助。谢谢
postgresql-json ×10
postgresql ×9
json ×6
sql ×3
jsonb ×2
array-agg ×1
indexing ×1
jackson ×1
java ×1
jsonschema ×1
nosql ×1
recursion ×1
spring-boot ×1
sql-order-by ×1