【问题标题】:How to get rows based on a certain value in one of the columns?如何根据其中一列中的某个值获取行?
【发布时间】:2023-03-26 01:21:01
【问题描述】:

请看下表:

| text_id | text_title     | topics_weights            |
| ------- | ---------------| --------------------------|
| 112233  | Greetings      | {"(123,0.9)", "(456,0.8)"}|
| 112234  | Congratulations| {"(789,0.8)", "(101,0.5)"}|
| 112235  | Salutations    | {"(123,0.8)", "(102,0.8)"}|

我的问题是,如果我想获取 topic_weights 列中包含 123 的所有 text_ids,如何从该表中获取所有行? topic_weights 列中的数据类型看起来很复杂,我不知道如何创建条件。

感谢您的帮助!谢谢!

【问题讨论】:

  • 这还不清楚。请写一个更好的描述并给出示例数据和表定义来说明你想要什么。
  • 该列到底是什么数据类型?请edit您的问题并添加create table声明(请Formatted textno screen shots
  • 感谢您的 cmets!我试图澄清描述。
  • 看起来 topics_weights 可能是一个数组:如果是:stackoverflow.com/questions/7925050/… 将引导您到 postgresql.org/docs/current/arrays.html#ARRAYS-SEARCHING,其中显示 SELECT * FROM sal_emp WHERE 10000 = ANY (pay_by_quarter);,然后您可以执行 Select * FROM yourTable WHERE 123 = ANY (topics_Weights),但会做出很多假设
  • 非常感谢!!

标签: sql postgresql


【解决方案1】:

您可以使用 substr 和 position 来获取这些特定值

select * from test where substr(topics_weights ,position('{"(' in topics_weights)+3,position(',' in topics_weights)-(position('{"(' in topics_weights)+3)) ='123';

dbfiddle:https://www.db-fiddle.com/f/ozt4mkok2jRs6nfownFiQd/0

【讨论】:

  • 哇,看起来棒极了!谢谢!!
  • 请问,代码中的+3是什么意思?
  • 位置将返回“{”的位置加3跳过{"(和数字
  • 啊哈..我明白了!惊人的!谢谢!
猜你喜欢
  • 2013-03-28
  • 1970-01-01
  • 1970-01-01
  • 2018-06-10
  • 2021-03-31
相关资源
最近更新 更多