答案 0 :(得分:2)
您将此条件添加到order by
子句中的case表达式中,从而确保您首先获取这些记录:
SELECT TOP 500 *
FROM mytable
WHERE /* conditions */
ORDER BY CASE WHEN /* optional condition */ THEN 0 ELSE 1 END
答案 1 :(得分:0)
您可以使用联合clause
尝试以下查询
create table #temp (id int, Age int, Course varchar(20))
insert into #temp values (1, 25, 'BE'),(2, 30, 'BE'),(3, 40, 'BE'),(4, 22, 'BE'),(5, 25, 'B. Tech'),(6, 27, 'BE')
select Top 4 * from #temp
where age >= 25
union
select * from #temp
where (Course is null or Course = 'B. Tech')
union
select * from #temp
where age >= 25 OR (Course is null or Course = 'B. Tech')
输出如下所示
id Age Course
1 25 BE
2 30 BE
3 40 BE
5 25 B. Tech
6 27 BE
您可以在此处找到实况穹顶-Demo Data Matches