在另一个表中包含字段中包含文本的地方获取行?
我正在尝试搜索包含另一个表格信息的字段。我已经尝试了以下查询:
SELECT *
FROM table_1
WHERE text_field LIKE '%'||(SELECT other_text FROM table_2)||'%';
SELECT *
FROM table_1
WHERE text_field ~* '(SELECT other_text FROM table_2)';
不幸的是,我得到了:
错误:由用作表达式
的子查询返回的一排以上
:
table_1
ID | TIMESTAMP TIMESTAMP TIMESTAMP | TEXT_FIELD |
---|---|---|
100 | 2022-06-01 17:40:00 | 两次在12秒内完成了两次交易 |
101 | 2022-06-- 01 17:42:42 | 一项交易在5秒内完成 |
102 | 2022-06-02 03:24:23 | 15创建的记录和4个在94秒内删除 |
table> table_2
id | elet_text |
---|---|
1 | 94 |
2 | 12 |
,我想和查询表_1基于包含12或94的text_field,这将给我这个返回:
ID | Timestamp | Text_field |
---|---|---|
100 | 2022-06-06-01 17:40:00 | 2在12秒内完成 |
102 | 2022-06-06-02 03:24:23 | 15创建的记录和4秒在94秒内删除了 |
我查看了多个建议,但它们都围绕一个加入,但是我的text_field
永远不会等于我的elets_text
field。<<< br> 我已经查看了数组,但是table_2
中的每个条目都是不同的,而不是逗号限制的列表。每个text_field
条目都有可能为变化的长度,如我的示例所示,因此我不能仅在text_field
中选择第六个“空间删除”字段;我必须做一些版本的“包含”或“喜欢”。
这甚至可能吗?
I'm trying to search a field that contains information from another table. I've tried the following queries:
SELECT *
FROM table_1
WHERE text_field LIKE '%'||(SELECT other_text FROM table_2)||'%';
SELECT *
FROM table_1
WHERE text_field ~* '(SELECT other_text FROM table_2)';
Unfortunately, I get:
ERROR: more than one row returned by a subquery used as an expression
Example tables:
table_1
id | timestamp | text_field |
---|---|---|
100 | 2022-06-01 17:40:00 | Two Transactions completed in 12 seconds |
101 | 2022-06-01 17:42:42 | One Transaction completed in 5 seconds |
102 | 2022-06-02 03:24:23 | 15 Records created and 4 deleted in 94 seconds |
table_2
id | other_text |
---|---|
1 | 94 |
2 | 12 |
And I want to query table_1 based on text_field containing either 12 or 94, which would give me this return:
id | timestamp | text_field |
---|---|---|
100 | 2022-06-01 17:40:00 | 2 Transactions completed in 12 seconds |
102 | 2022-06-02 03:24:23 | 15 Records created and 4 deleted in 94 seconds |
I've looked at multiple suggestions but they all center around a JOIN, but my text_field
won't ever EQUAL my other_text
field.
I've looked at arrays, but each entry in table_2
is distinct, not a comma-delimited list. Each text_field
entry has the potential to be a varying length as indicated in my example, so I can't just select the 6th 'space-delimited' field in text_field
; I have to do some version of "contains" or "like".
Is this even possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
像当前接受的答案所建议的那样加入表,通常不是您想要的。那里的问题还不清楚。
如果
其他_text
包含 4 , 9 , 15 和 94 ,您的行“创建的15个记录在94秒内删除了4个” 将在结果中列出四次。使用 相反,要获得不同的结果行:使用了多个重复的匹配,它的速度也更快。
:
相关 > eleth_text 可以包含
喜欢
的特殊字符,或者regexp match〜
(无论使用哪个)您可能需要逃脱它们:"> escape funcords for drestions funcords for drestions funcords for dractions funcords or for Prestions corters faref 一边加入“也是:
这是:
Joining tables like the currently accepted answer suggests, is typically not what you want. The question is not entirely clear there.
If
other_text
contains 4, 9, 15, and 94, your row with "15 Records created and 4 deleted in 94 seconds" would be listed in the result four times. UseEXISTS
instead to get distinct result rows:With more than a few duplicate matches, it's faster, too.
Related:
If
other_text
can contain special characters forLIKE
or the regexp match~
(whichever you use) you may want to escape them:Aside: yes, that's a "join" too:
It's the less readable twin of:
See:
使用 cte (公共表达式)。
要完整,我应该提到您可以将其缩短为:
Update
我更喜欢第一个形式,因为这对我来说更清楚了。
Use a CTE(Common Table Expression).
To be complete I should mention you can shorten this to:
UPDATE
I prefer the first form as it makes it clearer to me what is going on.