gpt4 book ai didi

sql - 在sql server 2005中实现部分排序查询

转载 作者:行者123 更新时间:2023-12-02 23:29:39 25 4
gpt4 key购买 nike

我必须以某种方式显示记录,某些选定的记录应该排在前面。此后,同一个表中的另一条记录以排序方式出现。

例如,如果我选择 stateID = 5 的状态,则相应的记录应该排在第一位。之后,另一条记录应该以排序的方式出现。

为此,我尝试了 union,但它显示所有排序。

select state from statemaster where stateid=5
union all
select state from statemaster
where not stateid =5
order by state

谢谢

最佳答案

这个将使用CASE首先为您提供 stateid = 5 的状态,然后是其余状态。第二个排序标准是状态

Select state
From statemaster
Order By
Case When stateid = 5 Then 0 Else 1 End,
state

关于sql - 在sql server 2005中实现部分排序查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2498391/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com