select a.MemberID,b.Label,(select c.URL from URLTable c where c.URLID=a.URLID) from URLhitlog a inner join TaskURLS b ON a.URLID=b.URLID
where a.URLID in (select top(20) URLhitlog.URLID from URLhitlog group by URLhitlog.URLID order by count(URLhitlog.URLID)desc)。
有a,b,c三张表,均有URLID字段.另外a有MemberID,b有Label,c有URL字段。
现在我要在a表中找到count(URLID)排名在前20之内的URLID,再用找的这20个URLID分别去b,c表中找到其对应的Label,URL字段。
基本意思就是这样。
我写的这个sql可以完成这个功能,但是效率比较低。。所以希望哪位高手有更好的sql语句或者帮助优化一下。谢谢
sql语句只限一条。。不能拆分成两条
8 个解决方案
#1
Up!
#2
提供下
3张表的结构
3张表的结构
#3
其实方法应该都差不多
在连接字段加索引好了
在连接字段加索引好了
#4
select
a.MemberID,b.Label,c.url
from
URLhitlog a
inner join
TaskURLS b ON a.URLID=b.URLID
inner join
URLTable c on c.URLID=a.URLID
where
a.URLID in (select top(20) URLhitlog.URLID from URLhitlog group by URLhitlog.URLID order by count(URLhitlog.URLID)desc)
#5
a表的结构:Member ID, URLID
b表的结构:URLID,Label,TaskID
c表的结构:URLID,TaskID,URL,Variant
我那条sql语句貌似错了,按道理搜索出来应该是20条数据,但是我搜出来后却有好多条
b表的结构:URLID,Label,TaskID
c表的结构:URLID,TaskID,URL,Variant
我那条sql语句貌似错了,按道理搜索出来应该是20条数据,但是我搜出来后却有好多条
#6
支持UP!!
#7
可行
#8
參考之SQL關聯語句...
SELECT *,CONVERT(nvarchar(20),d.gzsj,108)as gzsj1,CONVERT(nvarchar(20),d.cysj,108)as cysj1,CONVERT(nvarchar(20),d.gzss,108)as gzss1,
convert(varchar(20),convert(datetime,CONVERT(nvarchar(20),d.gzsj,108))-convert(datetime,(select convert(varchar(5),convert(datetime,sum(convert(numeric(10,3),
convert(datetime,CONVERT(varchar(20),GETDATE(),111)+' '+convert(varchar(20),gzss,108))))),108)+':00' from cq_pg where BH=d.bh and rq=d.rq)),108) as cyss1 FROM (
select a.CB as cb,a.bmmc,b.XB as xb,a.bh,a.xm,a.rq,MAX(a.sj1)as sbsj,MAX(a.sj4) as xbsj,
MAX(a.gzsj) as gzsj,b.WO as wo,b.GX as gx,MAX(b.KSSJ)as pgstartsj,MAX(b.JSSJ)as pgstopsj,MAX(b.GZSS) as gzss,
MAX(a.gzsj)-MAX(b.GZSS )as cysj,b.REMARK as remark,MAX(c.KSSJ)as jbstartsj,MAX(c.JSSJ)as jbstopsj,MAX(c.JBSS)as jbss from RES_KQRB a
left join CQ_PG b on a.bh=b.BH and a.xm=b.XM and a.rq=b.RQ left join CQ_JB c on a.bh=c.ACCOUNT_ID and a.xm=c.FULL_NAME and a.rq=c.JBRQ
where b.RQ='2010/02/02' and a.CB='B區' and a.bmmc='制二5' and b.xb='53E' group by a.CB,a.bmmc,b.XB,a.bh,a.xm,a.rq,b.WO,b.GX,b.remark,c.JBSS) d
group by d.CB,d.bmmc,d.XB,d.bh,d.xm,d.rq,d.WO,d.GX,d.remark,d.JBSS,d.sbsj,d.xbsj,d.gzsj,d.pgstartsj,d.pgstopsj,d.gzss,d.cysj,d.jbstartsj,d.jbstopsj order by cyss1,xm
#1
Up!
#2
提供下
3张表的结构
3张表的结构
#3
其实方法应该都差不多
在连接字段加索引好了
在连接字段加索引好了
#4
select
a.MemberID,b.Label,c.url
from
URLhitlog a
inner join
TaskURLS b ON a.URLID=b.URLID
inner join
URLTable c on c.URLID=a.URLID
where
a.URLID in (select top(20) URLhitlog.URLID from URLhitlog group by URLhitlog.URLID order by count(URLhitlog.URLID)desc)
#5
a表的结构:Member ID, URLID
b表的结构:URLID,Label,TaskID
c表的结构:URLID,TaskID,URL,Variant
我那条sql语句貌似错了,按道理搜索出来应该是20条数据,但是我搜出来后却有好多条
b表的结构:URLID,Label,TaskID
c表的结构:URLID,TaskID,URL,Variant
我那条sql语句貌似错了,按道理搜索出来应该是20条数据,但是我搜出来后却有好多条
#6
支持UP!!
#7
可行
#8
參考之SQL關聯語句...
SELECT *,CONVERT(nvarchar(20),d.gzsj,108)as gzsj1,CONVERT(nvarchar(20),d.cysj,108)as cysj1,CONVERT(nvarchar(20),d.gzss,108)as gzss1,
convert(varchar(20),convert(datetime,CONVERT(nvarchar(20),d.gzsj,108))-convert(datetime,(select convert(varchar(5),convert(datetime,sum(convert(numeric(10,3),
convert(datetime,CONVERT(varchar(20),GETDATE(),111)+' '+convert(varchar(20),gzss,108))))),108)+':00' from cq_pg where BH=d.bh and rq=d.rq)),108) as cyss1 FROM (
select a.CB as cb,a.bmmc,b.XB as xb,a.bh,a.xm,a.rq,MAX(a.sj1)as sbsj,MAX(a.sj4) as xbsj,
MAX(a.gzsj) as gzsj,b.WO as wo,b.GX as gx,MAX(b.KSSJ)as pgstartsj,MAX(b.JSSJ)as pgstopsj,MAX(b.GZSS) as gzss,
MAX(a.gzsj)-MAX(b.GZSS )as cysj,b.REMARK as remark,MAX(c.KSSJ)as jbstartsj,MAX(c.JSSJ)as jbstopsj,MAX(c.JBSS)as jbss from RES_KQRB a
left join CQ_PG b on a.bh=b.BH and a.xm=b.XM and a.rq=b.RQ left join CQ_JB c on a.bh=c.ACCOUNT_ID and a.xm=c.FULL_NAME and a.rq=c.JBRQ
where b.RQ='2010/02/02' and a.CB='B區' and a.bmmc='制二5' and b.xb='53E' group by a.CB,a.bmmc,b.XB,a.bh,a.xm,a.rq,b.WO,b.GX,b.remark,c.JBSS) d
group by d.CB,d.bmmc,d.XB,d.bh,d.xm,d.rq,d.WO,d.GX,d.remark,d.JBSS,d.sbsj,d.xbsj,d.gzsj,d.pgstartsj,d.pgstopsj,d.gzss,d.cysj,d.jbstartsj,d.jbstopsj order by cyss1,xm