繁体   English   中英

Select 搜索后具有相同列值的所有记录

[英]Select all records with same column value after search

我有一个名为“表格”的表格,看起来像这样

ID entry_id 内容
1 1 文本
2 1 兰德txt
3 1 另一个兰特txt
4 2 新条目

我需要根据内容搜索此表(例如SELECT * FROM forms WHERE content LIKE 'text'; ),如果找到该记录,则需要选择具有相同entry_id的所有其他记录。

例如,如果通过搜索查询找到 ID 为 1 的记录,则 ID 为 2 和 3 的记录也应被选中,因为它们属于同一条目。

SELECT t2.* 
FROM forms t1
JOIN forms t2 USING (entry_id) 
WHERE t1.content LIKE 'text';

请记住 - 不带模式符号的 LIKE 仅搜索直接匹配(即等于= )。

一种方法使用exists

select f.*
from forms f
where exists (select 1
              from forms f2
              where f2.id = f.id and
                    f.content like 'text'
             );

您还可以使用 window 函数:

select f.*
from (select f.*,
             sum( f.content like 'text' ) over (partition by id) as num_text
      from forms f
     ) f
where num_text > 0;

尝试:

SELECT *
FROM forms AS f1 INNER JOIN forms AS f2 ON f1.entry_id = f2.entry_id
WHERE f1.content LIKE 'text'

您的查询应该是这样的。 您可以使用嵌套查询。

SELECT * FROM forms
WHERE entry_id IN
(SELECT entry_id FROM forms WHERE content LIKE '%text%')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM