在另一个表中包含字段中包含文本的地方获取行?

发布于 2025-02-06 03:00:45 字数 2390 浏览 4 评论 0原文

我正在尝试搜索包含另一个表格信息的字段。我已经尝试了以下查询:

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

IDTIMESTAMP TIMESTAMP TIMESTAMPTEXT_FIELD
1002022-06-01 17:40:00两次在12秒内完成了两次交易
1012022-06-- 01 17:42:42一项交易在5秒内完成
1022022-06-02 03:24:2315创建的记录和4个在94秒内删除

table> table_2

idelet_text
194
212

,我想和查询表_1基于包含12或94的text_field,这将给我这个返回:

IDTimestampText_field
1002022-06-06-01 17:40:002在12秒内完成
1022022-06-06-02 03:24:2315创建的记录和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

idtimestamptext_field
1002022-06-01 17:40:00Two Transactions completed in 12 seconds
1012022-06-01 17:42:42One Transaction completed in 5 seconds
1022022-06-02 03:24:2315 Records created and 4 deleted in 94 seconds

table_2

idother_text
194
212

And I want to query table_1 based on text_field containing either 12 or 94, which would give me this return:

idtimestamptext_field
1002022-06-01 17:40:002 Transactions completed in 12 seconds
1022022-06-02 03:24:2315 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 技术交流群。

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

发布评论

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

评论(2

水溶 2025-02-13 03:00:45

像当前接受的答案所建议的那样加入表,通常不是您想要的。那里的问题还不清楚。

如果其他_text包含 4 9 15 94 ,您的行“创建的15个记录在94秒内删除了4个” 将在结果中列出四次。使用 相反,要获得不同的结果行:

SELECT t1.*
FROM   table_1 t1 
WHERE  EXISTS (SELECT FROM table_2 t2 WHERE t1.text_field ~ t2.other_text)
ORDER  BY t1.id;  -- optional

使用了多个重复的匹配,它的速度也更快。

相关 > eleth_text 可以包含喜欢的特殊字符,或者regexp match (无论使用哪个)您可能需要逃脱它们:

"> escape funcords for drestions funcords for drestions funcords for dractions funcords or for Prestions corters faref 一边加入“也是:

SELECT * FROM a,b WHERE a.x LIKE b.y;

这是:

SELECT * FROM a JOIN b ON a.x LIKE b.y;

  • 在Postgres中的意思是什么?

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. Use EXISTS instead to get distinct result rows:

SELECT t1.*
FROM   table_1 t1 
WHERE  EXISTS (SELECT FROM table_2 t2 WHERE t1.text_field ~ t2.other_text)
ORDER  BY t1.id;  -- optional

With more than a few duplicate matches, it's faster, too.

Related:

If other_text can contain special characters for LIKE or the regexp match ~ (whichever you use) you may want to escape them:

Aside: yes, that's a "join" too:

SELECT * FROM a,b WHERE a.x LIKE b.y;

It's the less readable twin of:

SELECT * FROM a JOIN b ON a.x LIKE b.y;

See:

牵你手 2025-02-13 03:00:45

使用 cte (公共表达式)。

WITH ot as(
   SELECT other_text FROM table_2)
SELECT * 
FROM ot, table_1 
WHERE text_field LIKE '%'|| ot.other_text||'%';

要完整,我应该提到您可以将其缩短为:

SELECT * 
FROM table2, table_1 
WHERE text_field LIKE '%'|| table2.other_text||'%';

Update

我更喜欢第一个形式,因为这对我来说更清楚了。

Use a CTE(Common Table Expression).

WITH ot as(
   SELECT other_text FROM table_2)
SELECT * 
FROM ot, table_1 
WHERE text_field LIKE '%'|| ot.other_text||'%';

To be complete I should mention you can shorten this to:

SELECT * 
FROM table2, table_1 
WHERE text_field LIKE '%'|| table2.other_text||'%';

UPDATE

I prefer the first form as it makes it clearer to me what is going on.

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