繁体   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