表:test
id style
a1 1
a2 1
a1 2
b1 2
a1 3
b1 3
c1 3
求STYLE=3,不包含在STYLE=(1,2)内的ID号。
sql文:
select t.id
from test t
where t.style = '3'
and not exists (select t1.id
from test t1
where t1.style = '1'
or t1.style = '2')
当数据量比较多时,检索速度非常慢,问一下高手有没有别的写法,可以改善,谢谢。
id style
a1 1
a2 1
a1 2
b1 2
a1 3
b1 3
c1 3
求STYLE=3,不包含在STYLE=(1,2)内的ID号。
sql文:
select t.id
from test t
where t.style = '3'
and not exists (select t1.id
from test t1
where t1.style = '1'
or t1.style = '2')
当数据量比较多时,检索速度非常慢,问一下高手有没有别的写法,可以改善,谢谢。
from test t
where t.style = '3' and
not exists (select t1.id from test t1 where t.id=t1.id and (t1.style = '1' or t1.style = '2'))如果数据量大,那么要分别对id,style建立索引