【问题标题】:Postgresql - Get Distinct Records on the basis of Json ValuesPostgresql - 根据 Json 值获取不同的记录
【发布时间】:2016-11-06 21:36:25
【问题描述】:

场景:我有一个表,其值以 Json 格式存储在其上 其中一列。我期望结果得到不同的或 基于值的唯一记录(json列的值之一 value) 以 json 格式存储。

My table have 3 columns :
1. Id (integer) 
ex. value - 20
2. Description (text) 
ex.value - {"first_name":"John","last_name":"Doe","company_id":"26"}
3. Created (integer)

现在我想要基于 company_id 值的 Distinct 记录,PostgreSQL 中是否有任何功能可满足此类要求?

技术信息:

Postgresql 版本 - 9.5

【问题讨论】:

  • 您可以使用DISTINCT ON select distinct on (ex.value ->> 'company_id')(或select distinct on (ex.value -> 'company_id'),如果您有jsonb),但不能保证其他列中的值(除非您提供额外的order by 表达式。

标签: php sql json postgresql distinct


【解决方案1】:

Postgres 9.5(以及一些早期版本)具有一些对查询 JSON 文本的内置支持。尝试以下查询:

SELECT *
FROM yourTable
WHERE Description->>'company_id' = 'some_value'

Documentation

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-13
    • 2022-11-25
    • 2014-01-06
    • 1970-01-01
    • 2022-12-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多