标签: postgresql

对于 Postgres,有效地获得点 Y 的 X 距离内的点?

我在 Postgres 中有这个表,如下所示:

create table locations
(
    id serial not null
        constraint games_pkey
            primary key,
    name varchar,
    location point,
)
;
Run Code Online (Sandbox Code Playgroud)

我想根据另一个位置point( x, y )作为纬度和经度进行查询;我只需要返回距离该位置 1 公里以内的记录。如何有效地选择符合该标准的记录?

我只需要在局部范围内精确的距离,因此地球曲率对我的应用程序来说不应该是一个问题,但是在这种情况下,POINT 或 GIS 是否有性能优势?

我在 stackoverflow 上发现了这个问题,它似乎做了类似的事情,但接受的答案只有代码,也没有说明它是如何工作的:https : //stackoverflow.com/questions/37827468/find-the-nearest-location-by -latitude-and-longitude-in-postgresql

postgresql spatial postgis

3
推荐指数
1
解决办法
456
查看次数

从一个表插入数据,然后获取该插入的主键并将其放入另一个表中

我有一个快速的问题。我有一个表用户

 -----------------------------
| ID | first_name | last_name |
 -----------------------------
Run Code Online (Sandbox Code Playgroud)

和另一个表,如 users.login

 ------------------
| users_ID | EMAIL |
 ------------------
Run Code Online (Sandbox Code Playgroud)

我在第一个表中的 id 从某个点自动递增,比如 160,000。如何从第一个表中获取 id,将其保存为一个值,然后将该值放入第二个表的 id 中?users.login 中的 id 是 users.id 的外键。

到目前为止我有

INSERT INTO public.users (first_name, last_name)
                VALUEs ($1,$2) RETURNING ID;
INSERT INTO public.users_login(email, ID)
                Values($1, id) 
Run Code Online (Sandbox Code Playgroud)

这行得通吗?我是 postgres 的新手。

postgresql

3
推荐指数
1
解决办法
309
查看次数

每天计算唯一(不同)用户

我是新手,psql并且对计算每天唯一的首次用户数量很感兴趣。

我的表只有两列 - user_id(一个用户每天可以多次使用),start_time这是指示使用时间的时间戳。计数应仅包括当天 min(start_time) 下降的用户。

为什么以下查询不起作用?难道不能一次在两个变量上选择不同的吗?

SELECT COUNT (DISTINCT min(start_time::date), user_id), 
       start_time::date as date 
FROM mytable 
GROUP BY date
Run Code Online (Sandbox Code Playgroud)

产生

错误:函数count(date, integer)不存在

输出看起来像这样

        date count
1 2017-11-22    56
2 2017-11-23    73
3 2017-11-24    13
4 2017-11-25    91
5 2017-11-26   107
6 2017-11-27    33...
Run Code Online (Sandbox Code Playgroud)

关于如何计算不同的 min Date 和 user_id 然后在 psql 中按日期分组的任何建议将不胜感激。

postgresql count distinct

3
推荐指数
1
解决办法
1万
查看次数

PostgreSQL - 无法更新 pg_database

我无法使用用户 postgres 更新表 pg_database。正如您在下面看到的,我能够运行 SQL 查询,但是没有任何变化。

postgres=# select datname, datdba, datistemplate, datallowconn from pg_database;
    datname     | datdba | datistemplate | datallowconn
----------------+--------+---------------+--------------
 postgres       |     10 | f             | t
 template1      |     10 | t             | t
 template0      |     10 | t             | f
 my_template_1  |     10 | f             | t
(4 rows)

postgres=# UPDATE pg_database set datistemplate=true, datallowconn=false where datname='my_template_1' ;
UPDATE 1
postgres=# select datname, datdba, datistemplate, datallowconn from pg_database;
    datname     | datdba | datistemplate | datallowconn
----------------+--------+---------------+--------------
 postgres …
Run Code Online (Sandbox Code Playgroud)

postgresql postgresql-9.6

3
推荐指数
1
解决办法
1524
查看次数

使用更大的运算符在 jsonb 数组中搜索嵌套值

这是表定义(简化):

CREATE TABLE documents (
    document_id int4 NOT NULL GENERATED BY DEFAULT AS IDENTITY,
    data_block jsonb NULL
);
Run Code Online (Sandbox Code Playgroud)

示例值:

INSERT INTO documents (document_id, data_block)
VALUES
   (878979, 
    '{"COMMONS": {"DATE": {"value": "2017-03-11"}},
     "PAYABLE_INVOICE_LINES": [
         {"AMOUNT": {"value": 52408.53}}, 
         {"AMOUNT": {"value": 654.23}}
     ]}')
 , (977656, 
    '{"COMMONS": {"DATE": {"value": "2018-03-11"}},
     "PAYABLE_INVOICE_LINES": [
         {"AMOUNT": {"value": 555.10}}
     ]}');
Run Code Online (Sandbox Code Playgroud)

我想搜索其中一个'PAYABLE_INVOICE_LINES'元素包含'value'大于 1000.00 的所有文档。

我的查询是

select *
from documents d
cross join lateral jsonb_array_elements(d.data_block -> 'PAYABLE_INVOICE_LINES') as pil 
where (pil->'AMOUNT'->>'value')::decimal > 1000
Run Code Online (Sandbox Code Playgroud)

但是,由于我想限制为 50 个文档,因此我必须对document_id …

postgresql performance index-tuning json postgresql-10 postgresql-performance

3
推荐指数
1
解决办法
2万
查看次数

列必须出现在 GROUP BY 子句中或用于聚合函数中

我有一个包含 col1、col2、col3 列的简单表格。都不可为空。

我想删除元组 (col1, col2) 有多个条目的所有行。背景:应添加 (col1, col2) 的唯一约束。

drop table mytable;

create table mytable (
    col1 integer not null,
    col2 integer not null,
    col3 integer not null);

-- rows to delete
insert into mytable values (1, 1, 1);
insert into mytable values (1, 1, 2);

-- rows to keep
insert into mytable values (2, 2, 1);
insert into mytable values (2, 3, 2);



delete from mytable where 
(col1, col2) in  (
    select col1, col2 from mytable  
    group by …
Run Code Online (Sandbox Code Playgroud)

postgresql postgresql-9.3 postgresql-10

3
推荐指数
1
解决办法
6202
查看次数

UPSERT 和 MERGE 的区别?

从 PostgreSQL 维基,

MERGE通常用于合并两个表,并在 2003 SQL 标准中引入。该REPLACE声明(一个MySQL扩展)或UPSERT序列尝试一个UPDATE或失败时INSERT。这类似于UPDATE,然后对于不匹配的行,INSERT。并发访问是否允许可能导致行丢失的修改与实现无关。

进一步的 PostgreSQLINSERT ... ON CONFLICT DO NOTHING/UPDATEUPSERT的形式销售,并在 9.5 中添加

那是MERGE什么?它是如何融入这个组合的?

postgresql terminology merge upsert

3
推荐指数
1
解决办法
7828
查看次数

从 INNER JOINed 3 个不同表中删除所有行

我有一个疑问。我想删除这 3 个不同表中的所有选定行 由于我有很多 INNER 连接,我无法弄清楚。我的目标是删除这些卖家 ID 的所有内容。

SELECT *
FROM orders a
 INNER JOIN order_items b ON a.order_id = b.order_id
 INNER JOIN order_item_histories c ON c.order_item_id = b.order_item_id
WHERE a.seller_id IN (1, 3)
Run Code Online (Sandbox Code Playgroud)

版本 Postgres 10.3

我试过这个,但我无法成功。

DELETE
FROM  
   USING orders
   USING order_items,
   USING order_item_histories
WHERE orders.order_id = order_items.order_id AND order_items.order_item_id = order_item_histories.order_item_id
AND  orders.seller_id IN (1, 3)
Run Code Online (Sandbox Code Playgroud)

postgresql performance postgresql-10 query-performance

3
推荐指数
2
解决办法
1万
查看次数

使用两个不同的 where 条件更新查询

我想设置statusfalse所有行除一人外,应应设置为true。我怎样才能做到这一点?

这是我试过的查询:

UPDATE students set status = false where status = true and 
set status = true where _id = 1;
Run Code Online (Sandbox Code Playgroud)

postgresql update

3
推荐指数
1
解决办法
434
查看次数

Postgres,如何将“0”值插入串行?

serial我的应用程序表中有一个ID。但是,我想插入一个0id 记录来表示全局。

有没有一种方法可以将它插入我的桌子而不影响计数器?

postgresql insert sequence

3
推荐指数
1
解决办法
3186
查看次数