SQL Server:IN 语句中的 ORDER BY 参数 [英] SQL Server: ORDER BY parameters in IN statement

查看:44
本文介绍了SQL Server:IN 语句中的 ORDER BY 参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 SQL 语句,如下所示:

I have a SQL statement that is the following:

SELECT A.ID, A.Name 
FROM Properties A 
WHERE A.ID IN (110, 105, 104, 106)

当我运行这个SQL语句时,输出会根据IN列表自动按ID排序并返回

When I run this SQL statement, the output is ordered according to the IN list by ID automatically and returns

   104 West
   105 East
   106 North
   110 South

我想知道是否可以按照参数在 IN 子句中列出的顺序进行排序.所以它会返回

I want to know if it is possible to order by the order the parameters are listed within the IN clause. so it would return

 110 South
 105 East
 104 West
 106 North

推荐答案

我认为在 SQL Server 中最简单的方法是使用 JOINVALUES:

I think the easiest way in SQL Server is to use a JOIN with VALUES:

SELECT p.ID, p.Name
FROM Properties p JOIN
     (VALUES (110, 1), (105, 2), (104, 3), (106, 4)) ids(id, ordering)
     ON p.id = a.id
ORDER BY ids.ordering;

这篇关于SQL Server:IN 语句中的 ORDER BY 参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆