select * from table1 where flag=1 and username in (select name from table2 where forbid=no and joinstate in(0,1) and group=12)select * from table1,table2 where flag=1 and table1.username=table2.name and forbid='no' and joinstate in(0,1) and group=12
select a.* from table1 a
where a.flag = 1
and table1.username = table2.name
and a.forbid = 'no'
and a.joinstate = 0
and a.group = 12
, and exists (select 1 form table2 b where a.username = b.name)
union all
select a.* from table1 a
where a.flag = 1
and table1.username = table2.name
and a.forbid = 'no'
and a.joinstate = 1
and a.group = 12
, and exists (select 1 form table2 b where a.username = b.name)
LZ 这两个执行效率都不高,要学会开执行计划
可以使用exists,提高执行效率,改下你的第一个SQLselect * from table1 where flag=1 and exists (select name from table2 where
table1.username=table2.username and forbid=no and joinstate in(0,1) and group=12)
凡关于SQL优化的,请给出数据量大小,数据分布,索引建立情况,统计数据,执行计划~~~~~