在项目中。碰到一个问题。。
如下图。蓝色的如公务员、基础教育等属于一张表Category。小黑色的是从表TB_Classinfo中查出。两者通过category对应。通过一条sql语句实现。如下
with topc AS
(
SELECT * from(
select top 4 ROW_NUMBER() over (order by Orders desc) as rownum,ID from Category_30
) t1
)
select t.Class_Name,t.CreateTime,b.CategoryName from (
select top 5 Class_Name,Category,CreateTime from TB_ClassInfo
where Category in (select id from topc where rownum=1)
union
select top 5 Class_Name,CreateTime from TB_ClassInfo
where Category in (select id from topc where rownum=2)
union
select top 5 Class_Name,CreateTime from TB_ClassInfo
where Category in (select id from topc where rownum=3)
union
select top 5 Class_Name,CreateTime from TB_ClassInfo
where Category in (select id from topc where rownum=4)
) t
inner join Category_30 b
on t.Category=b.ID
select top 20 t.Class_Name,b.CategoryName from (
select Class_Name,CreateTime from TB_ClassInfo
where Category in (SELECT ID from(
select top 4 ROW_NUMBER() over (order by Orders desc) as rownum,ID from Category_30
order by Orders desc) t1)
) t
inner join Category_30 b
on t.Category=b.ID