大神进来 帮我优化一下SQL好吗?
select
cus_fielddata.seqorder,cus_fielddata.scopeid,cus_fielddata.scope,cus_fielddata.field5,cus_fielddata.field4,cus_fielddata.field3,cus_fielddata.field2,cus_fielddata.field1,cus_fielddata.field0,HrmResource.* from dbo.cus_fielddata,HrmResource where cus_fielddata.scopeid=1 and HrmResource.id=cus_fielddata.id
union
select
null as seqorder,null as scopeid,null as scope,null as field5,null as field4,null as field3,null as field2,null as field1,null as field0,HrmResource.* from HrmResource where (select count(*) as num from dbo.cus_fielddata where scopeid=1 and HrmResource.id=cus_fielddata.id)=0 order by id asc
看看,我为了个查询,用了两个Select,我还为了合并这两个查询结果,我还得把表1和表1的字段一个一个都凑齐。。。然后再用union进行合并~
我知道的,一定不可能这么傻,一定有更好的方法 =、=
------解决方案--------------------不知道这样对不对
select cus_fielddata.seqorder,cus_fielddata.scopeid,cus_fielddata.scope,cus_fielddata.field5,cus_fielddata.field4,cus_fielddata.field3,cus_fielddata.field2,cus_fielddata.field1,cus_fielddata.field0,HrmResource.*
from HrmResource left join (select * from dbo.cus_fielddata where cus_fielddata.scopeid=1) cus_fielddata on HrmResource.id=cus_fielddata.id
order by order by id asc