22,210
社区成员
发帖
与我相关
我的任务
分享
SELECT
a.*
FROM
table1 a
WHERE
a.ID IN
(
SELECT
TOP 5 b.ID
FROM
table1 b
WHERE
b.title<>'abc'
ORDER BY NEWID()
)
OR a.title = 'abc'
select * from Table1 where title = 'abc'
union all
select top 5 * from Table1 where title <> 'abc' order by NEWID
with table1 as
(
select 1 id, 'a' title union all
select 2 id, 'b' title union all
select 3 id, 'c' title union all
select 4 id, 'd' title union all
select 5 id, 'e' title union all
select 6 id, 'abc' title union all
select 7 id, 'g' title union all
select 8 id, 'h' title union all
select 9 id, 'i' title union all
select 10 id, 'j' title union all
select 11 id, 'k' title union all
select 12 id, 'l' title union all
select 13 id, 'abc' title union all
select 14 id, 'n' title union all
select 15 id, 'o' title union all
select 16 id, 'p' title union all
select 17 id, 'q' title union all
select 18 id, 'abc' title union all
select 19 id, 's' title union all
select 20 id, 't' title
)
select * from table1 where title='abc'
union all
select * from (select top 5 * from table1 where title<>'abc' order by NEWID()) aa
SELECT TOP 1 *
FROM table1
WHERE title = 'abc'
UNION ALL
SELECT *
FROM ( SELECT TOP 5 *
FROM table1
WHERE title <> 'abc'
ORDER BY Newid()
) b