34,588
社区成员
发帖
与我相关
我的任务
分享
select *
from(
select Id,Mid,Picture,ids=row_number()over(partition by Mid order by getdate())
from News
where Mid in (118,260,123,144,143,288) and Picture<>''
)a
where ids<3
order by Mid,Id
union all
select *
from(
select Id,Mid,Picture,ids=row_number()over(partition by Mid order by getdate())
from News
where Mid in (118,260,123,144,143,288) and Picture=''
)b
where ids<8
order by Mid,Id
;
WITH cte
AS ( SELECT Id ,
Mid ,
Picture
FROM News
WHERE Mid IN ( 118, 260, 123, 144, 143, 288 )
)
SELECT *
FROM ( SELECT *
FROM ( SELECT * ,
ids = ROW_NUMBER() OVER ( PARTITION BY Mid ORDER BY GETDATE() )
FROM cte
WHERE Picture <> ''
) a
WHERE ids < 3
UNION ALL
SELECT *
FROM ( SELECT * ,
ids = ROW_NUMBER() OVER ( PARTITION BY Mid ORDER BY GETDATE() )
FROM cte
WHERE Picture = ''
) b
WHERE ids < 8
) c
ORDER BY mid ,
id