选择顺序为IN子句的记录

时间:2010-07-22 07:30:28

标签: sql tsql

我有

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

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

我可以用

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

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

8 个答案:

答案 0 :(得分:16)

我知道这有点晚了,但最好的方法是

SELECT *
FROM   Table1
WHERE  Col1 IN( 4, 2, 6 )
ORDER  BY CHARINDEX(CAST(Col1 AS VARCHAR), '4,2,67')

或者

SELECT CHARINDEX(CAST(Col1 AS VARCHAR), '4,2,67')s_order,
       *
FROM   Table1
WHERE  Col1 IN( 4, 2, 6 )
ORDER  BY s_order

答案 1 :(得分:6)

你有几个选择。最简单的方法是将IN参数(它们是右边的参数)按照收到它们的顺序放在一个单独的表中,然后按表ORDER BY。

答案 2 :(得分:3)

解决方案就在这条线上:

SELECT * FROM Table1 
WHERE Col1 IN(4,2,6)
ORDER BY 
    CASE Col1 
    WHEN 4 THEN 1
    WHEN 2 THEN 2
    WHEN 6 THEN 3
    END

答案 3 :(得分:1)

select top 0 0 'in', 0 'order' into #i
insert into #i values(4,1)
insert into #i values(2,2)
insert into #i values(6,3)

select t.* from Table1 t inner join #i i on t.[in]=t.[col1] order by i.[order]

答案 4 :(得分:1)

用表替换IN值,包括查询中使用的排序顺序列(并确保将排序顺序公开给调用应用程序):

WITH OtherTable (Col1, sort_seq)
     AS 
     (
      SELECT Col1, sort_seq
        FROM (
              VALUES (4, 1), 
                     (2, 2), 
                     (6, 3)
             ) AS OtherTable (Col1, sort_seq)
     )
SELECT T1.Col1, O1.sort_seq
  FROM Table1 AS T1
       INNER JOIN OtherTable AS O1
          ON T1.Col1 = O1.Col1
 ORDER 
    BY sort_seq;

在存储过程中,而不是CTE,将值拆分为表(临时基表,临时表,返回表的函数等),并根据需要填充排序列。

答案 5 :(得分:1)

我找到了另一个解决方案。它与oneday时的答案类似,但它的时间有点短。

SELECT sort.n, Table1.Col1
FROM (VALUES (4), (2), (6)) AS sort(n)
JOIN Table1
ON Table1.Col1 = sort.n

答案 6 :(得分:0)

我正在考虑这个问题有两种不同的方法,因为我无法确定这是编程问题还是数据架构问题。查看下面的代码,其中包含“着名的”电视动物。假设我们正在追踪海豚,马,熊,狗和猩猩。我们想在我们的查询中只返回马,熊和狗,我们希望熊在马前排序以排除狗。我个人倾向于将此视为架构问题,但可以将其视为编程问题。如果您有疑问,请告诉我。

CREATE TABLE #AnimalType (
  AnimalTypeId      INT         NOT NULL PRIMARY KEY
  , AnimalType      VARCHAR(50) NOT NULL
  , SortOrder       INT         NOT NULL)

INSERT INTO #AnimalType VALUES (1,'Dolphin',5)
INSERT INTO #AnimalType VALUES (2,'Horse',2)
INSERT INTO #AnimalType VALUES (3,'Bear',1)
INSERT INTO #AnimalType VALUES (4,'Dog',4)
INSERT INTO #AnimalType VALUES (5,'Orangutan',3)

CREATE TABLE #Actor (
  ActorId           INT         NOT NULL PRIMARY KEY
  , ActorName       VARCHAR(50) NOT NULL
  , AnimalTypeId    INT         NOT NULL)

INSERT INTO #Actor VALUES (1,'Benji',4)
INSERT INTO #Actor VALUES (2,'Lassie',4)
INSERT INTO #Actor VALUES (3,'Rin Tin Tin',4)
INSERT INTO #Actor VALUES (4,'Gentle Ben',3)
INSERT INTO #Actor VALUES (5,'Trigger',2)
INSERT INTO #Actor VALUES (6,'Flipper',1)
INSERT INTO #Actor VALUES (7,'CJ',5)
INSERT INTO #Actor VALUES (8,'Mr. Ed',2)
INSERT INTO #Actor VALUES (9,'Tiger',4)



/* If you believe this is a programming problem then this code works */
SELECT  *
FROM    #Actor a
WHERE   a.AnimalTypeId IN (2,3,4)
ORDER   BY case when a.AnimalTypeId = 3 then 1
                when a.AnimalTypeId = 2 then 2
                when a.AnimalTypeId = 4 then 3 end

/* If you believe that this is a data architecture problem then this code works */              
SELECT  *
FROM    #Actor a
JOIN    #AnimalType at ON a.AnimalTypeId = at.AnimalTypeId
WHERE   a.AnimalTypeId IN (2,3,4)
ORDER   BY at.SortOrder


DROP TABLE #Actor
DROP TABLE #AnimalType

答案 7 :(得分:0)

ORDER BY CHARINDEX(','+convert(varchar,status)+',' ,   
  ',rejected,active,submitted,approved,')

只需在要查找子字符串索引的字符串之前和之后添加逗号,或者可以说第二个参数。

CHARINDEX的第一个参数也被,(逗号)包围。