紧急求助一个SQL查询语句!
林塚 2007-03-22 03:32:43 需求如下:
表table1(phone)
表table2(phone,style1,style2,indatetime)
用一条select语句关联查询出table1中的Style1='a'的phone数量、Style1='a'且Style2='b'的phone数量、每个style1='a'的phone最大的indatetime。我原来是这样写的:
select a.phone,b.num1,c.num2,b.indatetime from table1 a
left join (select count(phone) as num1,phone,max(indatetime) as indatetime from table2 where Style1='a' group by phone) b on a.phone=b.phone
left join (select count(phone) as num2,phone from table2 where style1='a' and style2='b' group by phone) c on a.phone=c.phone
现在问题出在:table1和table2在不同的服务器上,我这样通过两个左连接table2速度很慢(实际要7-8秒),每次都超时(程序允许的查询时间是3秒)。请问大家还有没有更好更快的写法?