我正在使用SQLite,我有一个名字的表:
[Data]
"John"
"John"
"John"
"John.wane"
当我查询时:
SELECT * from [TABLE] Group By Data
我明白了:
John
John.wane
如何归档其他列中包含的列? 这个例子,“约翰”被包括在“约翰,.Wane”中 所以“John.Wane”不应出现在查询结果中。
答案 0 :(得分:0)
您正在寻找其他名称中未包含的名称。这有点困难。好吧,'John'在'John.wane'中,但它也在'Bobby.John.wane'中,甚至在'John.waned'中?
这是一个查询,只获取不存在包含该名称的其他名称条目的名称。根据您的意愿调整InStr
表达式。
select data
from mytable
where not exists
(
select *
from mytable other
where other.data <> mytable.data
and instr(other.data, mytable.data) > 0
);
答案 1 :(得分:0)
您可以使用以下内容:
SELECT CASE WHEN t2.Data IS NOT NULL THEN t2.Data ELSE t1.Data END
FROM Table1 AS t1
LEFT JOIN Table1 AS t2
ON t1.id <> t2.id AND t1.Data <> t2.Data AND t1.Data LIKE '%' || t2.Data || '%'
GROUP BY CASE WHEN t2.Data IS NOT NULL THEN t2.Data ELSE t1.Data END
答案 2 :(得分:-1)
此代码适用于我(#tmp是示例的临时表):
select 'John' as Data
into #tmp
union
select 'John' as Data
union
select 'John' as Data
union
select 'John' as Data
union
select 'John.Wane' as Data
select t1.Data
from #tmp t1
where t1.Data not in
(
select t3.Data
from #tmp t2
cross join #tmp t3
where t2.Data != t3.Data
and t3.Data not like REPLACE(t3.Data, t2.Data, '') + '%'
)
group by t1.Data
drop table #tmp