2016-04-11 20 views
0

diğer sonuçlar içerdiği sonuçları filtreleyin, İsimler konusunda bir tablo varSQL Sorgu: Ben olsun</p> <pre><code>SELECT * from [TABLE] Group By Data </code></pre> <p>:</p> <pre><code>[Data] "John" "John" "John" "John.wane" </code></pre> <p>Ben sorgulamak: Ben SQLite kullanıyorum

John 
John.wane 

Diğer sütunlarda bulunan sütunları nasıl filtrelerim? Bu örnekte "John", "John, .Wane" 'da bulunur, bu nedenle "John.Wane", sorgu sonucunda bulunmamalıdır.

+0

Sadece bir yan not : Hiçbir şeyi topladığınızda "GROUP BY" kullanmayın (yani MIN, MAX, COUNT vb. Kullanın). Sorgunuz, yerine [TABLO] 'dan DISTINCT verilerini seçmelidir. –

cevap

0

Diğer adlarda bulunmayan adları arıyorsunuz. Bu biraz zor. Tamam, 'John' 'John.wane' içinde, ama aynı zamanda 'Bobby.John.wane' veya hatta 'John.waned' de mi?

Yalnızca isimleri içeren başka ad girişleri bulunmayan adların alınmasıyla ilgili bir sorgulama. InStr ifadesini isteklerinize göre ayarlayın.

select data 
from mytable 
where not exists 
(
    select * 
    from mytable other 
    where other.data <> mytable.data 
    and instr(other.data, mytable.data) > 0 
); 
0

Böyle bir şey kullanabilirsiniz:

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 
-1
Bu kod (#tmp örneğin geçici tablodur) benim için çalışıyor

:

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 
İlgili konular