我无法弄清楚如何要求 SQLite 返回表中“列”包含 select 语句结果中找到的文本的所有条目。
如果这有意义的话,我希望它能像这样工作:
select * from table1`
where ("%" || (column) || "%") like in
("%" || (select different_column from table2) || "%")`
任何帮助将不胜感激。如果有人需要更多信息,请告诉我。
编辑:抱歉,我已经编辑了格式以使其更具可读性,并在下面添加了更多细节。我还删除了我的极其难以阅读的评论,转而只编辑这个问题。
table1(警报)包含一个名为“lookup_info”的列,它存储一个字符串。该字符串通常如下所示:
---
:account_description: Administrator
:alertable_class: Computer
:alertable_name: exampledotnet
:network_account_type:
:wmi:
:user: exampledomain\administrator
table2(设备)包含一个名为“name”的列,其中存储一个字符串。该字符串通常如下所示:
so-ws-rm101
我想要做的是返回表1(警报)中的所有条目,其中表1的“lookup_info”列不在其中的某个位置包含存储在表2(设备)中的表2的“名称”下的计算机名称”专栏。
基本上,有一些警报引用了不再存在的计算机名称。我想要这些警报。
谢谢。
编辑:我创建了一个名为alerts_fts的fts3表,其中包含警报表中所有条目的lookup_info列。这次我将使用全文搜索再次尝试搜索。这是我正在使用的查询:
select *
from alerts_fts
where alerts_fts.lookup_info match ( select group_concat(name, " OR ")
from Devices
group by "x")
这应该使查询看起来像:
select *
from alerts_fts
where alerts_fts.lookup_info match (cw-ws-example OR so-p2-132-example OR
... OR so-p4-lastexample)
group by "x")
当然,这仍然行不通。该查询不返回任何内容。我做错了什么?
最佳答案
我认为你需要通过连接来做到这一点:
select t1.**
from table1 t1 join
table2 t2
on t1.column like '%'||t2.column||'%'
如果可能存在多个匹配项,您需要添加distinct
:
select distinct t1.*
. . .
也许这就是您所需要的:
select t1.**
from table1 t1 join
table2 t2
on t1.column like '%'||t2.columnA||'%' or
t1.column like '%'||t2.columnB||'%' or
t1.column like '%'||t2.columnC||'%' or
. . .
根据您的编辑,我认为您需要左外连接,然后检查是否不匹配:
select table1.*
from table1 left outer join
table2
on t1.column like '%'||t2.column||'%'
where t2.column is null
关于SQLite:确定字符串的任何部分是否包含选择查询返回的任何一个(多个)字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15074953/