按 IN 子句的顺序选择记录 [英] Select records with order of IN clause

查看:30
本文介绍了按 IN 子句的顺序选择记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有

SELECT * FROM Table1 WHERE Col1 IN(4,2,6)

我想选择并返回我在 IN 子句中指定的指定顺序的记录(第一个显示记录,Col1=4,Col1=2,...)

I want to select and return the records with the specified order which i indicate in the IN clause (first display record with Col1=4, Col1=2, ...)

我可以使用

SELECT * FROM Table1 WHERE Col1 = 4
UNION ALL
SELECT * FROM Table1 WHERE Col1 = 6 , .....

但我不想使用它,因为我想将它用作存储过程而不是自动生成.

but I don't want to use that, cause I want to use it as a stored procedure and not auto generated.

推荐答案

您有几个选择.最简单的可能是将 IN 参数(它们是参数,对)按照您收到它们的顺序放在一个单独的表中,然后按该表进行 ORDER.

You have a couple of options. Simplest may be to put the IN parameters (they are parameters, right) in a separate table in the order you receive them, and ORDER BY that table.

这篇关于按 IN 子句的顺序选择记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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