簡體   English   中英

如何根據存儲在 Postgresql 列中的 json 數據獲取行

[英]how to fetch row on the basis of json data stored in a column in Postgresql

我有一張這樣的桌子

| is_selcted_cours   | content                                     | value |
|   (boolean)        |(json)                                       |       |
------------------------------------------------------------------------------
|    true   | {"date":"07-Apr-2020","amount":"5050","type":"CT"}    |   1    |
|    false  | {"date":"07-Jun-2020","amount":"50","type":"CT"}      |   1    |
|    true   | {"date":"10-Aug-2020","amount":"6050","type":"MT"}    |   1    |
|    false  | {"date":"07-Jun-2020","amount":"50","type":"CT"}      |   2    |
|    true   | {"date":"07-Apr-2020","amount":"5050","type":"GT"}    |   3    |
|    true   | {"date":"07-Apr-2020","amount":"5050","type":"GT"}    |   3    |
|    true   | {"date":"07-Apr-2020","amount":"5050","type":"GT"}    |   3    |

我想獲取所有重復的行(這里重復意味着包含is_selcted_cours is true並且the value of date, amount in the content column具有相同的值),就像在這個表中選擇的行將是1,5,6,7

我會為此使用 EXISTS 條件:

select d1.*
from data d1
where is_selcted_cours
and exists (select *
            from data d2
            where d1.content ->> 'date' = d2.content ->> 'date'
              and d1.content ->> 'amount' = d2.content ->> 'amount'
              and d2.is_selcted_cours = d1.is_selcted_cours
              and d1.id <> d2.id)

d1.id <> d2.id是不將行與自身進行比較所必需的。 id被假定為主要(或唯一)鍵列。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM