使用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';这样的东西会很好.但是使用临时表的方法也足够了.
我需要在PostgreSQL表中存储一个URL.保存具有未确定长度的URL的字段的最佳数据类型是什么?
提前致谢.
在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) 目前我正在使用 PostgreSQL Plus Advance Server 9.3,我刚刚创建了一个包含 blob 类型列的表,并且我使用 oid 数据类型来存储 blob 值。
现在我想创建一个 BEFORE UPDATE AND DELETE TRIGGER 来使用以下命令从 postgresql 的 pg_largeobject 表中删除孤立数据:
CREATE TRIGGER DEL_OID BEFORE UPDATE OR DELETE ON INFO_LOB
FOR EACH ROW EXECUTE PROCEDURE lo_manage (BLOB_VALUE_);
Run Code Online (Sandbox Code Playgroud)
但是当我尝试执行这个触发器时,出现错误:
ERROR: function lo_manage() does not exist
SQL state: 42883
Run Code Online (Sandbox Code Playgroud)
所以我无法理解我犯了什么错误。有人能帮帮我吗?谢谢
我有一个非常简单的表,其中包含一个timestampz存储UTC datetime值的start_date字段()。我需要返回调整为British Summer Time(BST)的表格中包含的日期,但是我遇到的问题是,AT TIME ZONE BST会将UTC日期增加1小时,而不管它们的值如何;如果日期发生在February(开始BST之前),则该UTC值为正确的值。如果日期值是从March(开始BST之后),则该UTC值需要+01添加到它。
表数据
2014-02-16 00:00:000+00
2014-04-16 00:00:000+00
SELECT start_date AT TIME ZONE 'BST' from t1
Run Code Online (Sandbox Code Playgroud)
将两个值加+ 01小时
2014-02-16 00:01:000
2014-04-16 00:01:000
Run Code Online (Sandbox Code Playgroud)
有没有简单的方法可以实现以下目标?
2014-02-16 00:00:000
2014-04-16 00:01:000
Run Code Online (Sandbox Code Playgroud) 我是postgresql的新手,我想知道postgresql中的OID有多少字节唯一整数?我正在使用postgresql版本9.3.
我有分组问题
我有一张桌子(其中有很多东西,但不相关)看起来像:
id user
0 1
1 1
2 1
3 2
4 2
5 2
6 1
7 1
Run Code Online (Sandbox Code Playgroud)
我正在尝试获得以下值:
user start end
1 0 2
2 3 5
1 6 7
Run Code Online (Sandbox Code Playgroud)
基本上,我需要用户的第一次和最后一次出现,而不是搞乱订单.我知道我需要使用OVER(PARTITION BY ...),但我从未使用它,也不确定如何构建此查询.如果我"按用户分区",它会忽略顺序.如果我"按id,用户分区"它再次返回错误.
我试过的例子(甚至没有尝试得到我需要的东西,但是一个中点,告诉我一旦我弄清楚"结束"部分怎么做):
SELECT user, count(user) over (partition by user):
user count
1 5
1 5
1 5
2 3
2 3
2 3
1 5
1 5
SELECT user, count(user) over (partition by id, user):
user count
1 1
1 1
1 1
2 1 …Run Code Online (Sandbox Code Playgroud) 我有一个包含locations以下列的表格:
latitudelongitude现在我想查询给定纬度/经度点的特定半径内的所有条目。
SELECT * FROM locations
WHERE ST_DWithin(
ST_MakePoint(longitude, latitude),
ST_MakePoint(-0.63098, 51.18291),
100
);
Run Code Online (Sandbox Code Playgroud)
上面的查询解释了我作为输入的数据以及我必须查询的数据。
有什么想法吗?