SQL 查询查找字符串包含特定 id 的重复项

发布于 2025-01-12 17:53:11 字数 187 浏览 2 评论 0原文

我有一个数据库表,其中一个字段(有效负载)是存储 JSON 对象的字符串。该 JSON 有多个属性。我想找到一种方法来查询有效负载 json-object 包含相同属性 id_o 值的所有条目以查找重复项。

因此,例如,如果存在多个有效负载字符串的 id_o 为“id_o:100”的条目,我想取回这些行。 我该怎么做?

提前致谢!

I have a database table where one field (payload) is a string where a JSON-object is stored. This JSON has multiple attributes. I would like to find a way to query all entries where the payload json-object contains the same value for the attribute id_o to find duplicates.

So for example if there existed multiple entries where id_o of the payload-string is "id_o: 100" I want to get these rows back.
How can I do this?

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

神回复 2025-01-19 17:53:11

我以前也遇到过类似的问题。
我使用regexp_substr

SELECT regexp_substr(yourJSONcolumn, '"id_o":"([^,]*)',0,1,'e')  end as   give_it_a_name

将“([^,])”中的逗号替换为“.”如果 id_0:100 之后有一个 .或您想要删除的其他内容。

I have faced similar issue like this before.
I used regexp_substr

SELECT regexp_substr(yourJSONcolumn, '"id_o":"([^,]*)',0,1,'e')  end as   give_it_a_name

the comma in "([^,])" can be replaced with a "." if after the id_0:100 has a . or something else that you want to remove.

笛声青案梦长安 2025-01-19 17:53:11

我认为将 json 存储在数据库中并不是一个好的体验。现在你的数据库需要标准化,如果你在数据库中创建一个新行,给它一个唯一的索引并将这个 id_o 属性存储在那里,那就太好了。

更新
这是我在另一个问题中发现的:

如果您确实希望能够添加任意数量的字段,而没有任何限制(除了任意文档大小限制),请考虑 NoSQL 解决方案,例如 MongoDB。

对于关系数据库:每个值使用一列。将 JSON blob 放入列中几乎无法进行查询(当您实际找到有效的查询时,速度会非常慢)。

关系数据库在索引时利用数据类型,并且旨在使用规范化结构来实现。

附带说明:这并不是说您永远不应该将 JSON 存储在关系数据库中。如果您要添加真实的元数据,或者您的 JSON 描述的是不需要查询且仅用于显示的信息,那么为所有数据点创建单独的列可能会有些过分。

i think storing json in database is not a good experience. Now your db needs a normalization, it will be good, if you create a new row in your db, give it a unique index and store this id_o property there.

UPDATE
here what i find in another question:

If you really want to be able to add as many fields as you want with no limitation (other than an arbitrary document size limit), consider a NoSQL solution such as MongoDB.

For relational databases: use one column per value. Putting a JSON blob in a column makes it virtually impossible to query (and painfully slow when you actually find a query that works).

Relational databases take advantage of data types when indexing, and are intended to be implemented with a normalized structure.

As a side note: this isn't to say you should never store JSON in a relational database. If you're adding true metadata, or if your JSON is describing information that does not need to be queried and is only used for display, it may be overkill to create a separate column for all of the data points.

空城仅有旧梦在 2025-01-19 17:53:11

我猜你的 JSON 看起来像这样: {..,"id_o":"100",..}

SELECT * FROM your_table WHERE your_column LIKE '%"id_o":"100"%'

I guess you JSON looks like this: {..,"id_o":"100",..}

SELECT * FROM your_table WHERE your_column LIKE '%"id_o":"100"%'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文