1

我有这个查询...

select * from selection_value where id = 14702
14702
14704
14724
14710
14738
14717
14719
14719
14738
14722
14721
14724
14725
14706
14730
14708
14731
14715
14708
14749
14752
14754
14755
14757
14795
14753
14760
14761
14759
14762
14763
14785
14764
14765
14785
14766
14768
14796
14771
14772
14774
14776
14764
14780
14781
14767
14784
14785
14786
14788
14789
14790
14791
14785
14772
14792
14796
14785
14797
14798
14799
14800
14802
14778
14803
14758
14765
14762
14781
14785
14786
14808
14793
14805
14807
14808

现在我知道这行不通,我只是想知道按此顺序获取所有这些 id 的数据的最佳方法是什么?

这可能吗?

4

5 回答 5

4

您可以创建一个派生表,其中包含要在一个列中匹配的值以及它们在另一列中的排序 - 将其连接到您的表并按排序列排序。

  select *
    from (
         select 1 ord, 14702 value union all
         select 2, 14704 union all
         select 3, 14724 union all
         select 4, 14710 union all
         select 5, 14738
         ) x
    join selection_value t on t.id = x.value
order by ord;
于 2012-11-07T18:56:15.247 回答
4

使用IN

SELECT * FROM selection_value WHERE id IN (14702, 14702, 14704, etc.) ORDER BY id

*编辑:*

要保留您拥有的特定订单,请创建一个新表:

CREATE TABLE2 (id INT IDENTITY(1,1), id_number int)

在此表中,按您需要对它们进行排序的顺序添加数字。然后,您可以使用类似以下查询的内容按您的特定顺序提取 ID:

SELECT selection_Value.* FROM selection_value JOIN Table2 ON selection_Value.id = table2.id_number ORDER BY Table2.id
于 2012-11-07T18:55:54.827 回答
2

使用IN逗号分隔值的子句。

select * from selection_value where id IN (14702,14703, so on) ORDER BY ID
于 2012-11-07T18:57:32.433 回答
1

你的问题不是很清楚,但也许:

SELECT * FROM selection_value 
WHERE id IN
      (SELECT id FROM table ORDER BY id ASC)

刚刚指出您的订单(您的查询是否需要从另一个表中提取数据?):

select * from selection_value where id IN (14702,14702,14704,14724,14710,14738,14717,14719,14719,14738,14722,14721,14724,14725,14706,14730,14708,14731,14715,14708,14749,14752,14754,14755,14757,14795,14753,14760,14761,14759,14762,14763,14785,14764,14765,14785,14766,14768,14796,14771,14772,14774,14776,14764,14780,14781,14767,14784,14785,14786,14788,14789,14790,14791,14785,14772,14792,14796,14785,14797,14798,14799,14800,14802,14778,14803,14758,14765,14762,14781,14785,14786,14808,14793,14805,14807,14808)
于 2012-11-07T18:57:25.427 回答
-1

只需将您的问题粘贴到记事本++中,在正则表达式模式下执行以下搜索和替换:

  • \r -> ''
  • \n -> \nunion all\nselect * from selection_value where id =

瞧!

select * from selection_value where id = 14702
union all
select * from selection_value where id =14702
union all
select * from selection_value where id =14704
union all
select * from selection_value where id =14724
union all
select * from selection_value where id =14710
union all
select * from selection_value where id =14738
union all
select * from selection_value where id =14717
union all
select * from selection_value where id =14719
union all
select * from selection_value where id =14719
union all
select * from selection_value where id =14738
union all
select * from selection_value where id =14722
union all
select * from selection_value where id =14721
union all
select * from selection_value where id =14724
union all
select * from selection_value where id =14725
union all
select * from selection_value where id =14706
union all
select * from selection_value where id =14730
union all
select * from selection_value where id =14708
union all
select * from selection_value where id =14731
union all
select * from selection_value where id =14715
union all
select * from selection_value where id =14708
union all
select * from selection_value where id =14749
union all
select * from selection_value where id =14752
union all
select * from selection_value where id =14754
union all
select * from selection_value where id =14755
union all
select * from selection_value where id =14757
union all
select * from selection_value where id =14795
union all
select * from selection_value where id =14753
union all
select * from selection_value where id =14760
union all
select * from selection_value where id =14761
union all
select * from selection_value where id =14759
union all
select * from selection_value where id =14762
union all
select * from selection_value where id =14763
union all
select * from selection_value where id =14785
union all
select * from selection_value where id =14764
union all
select * from selection_value where id =14765
union all
select * from selection_value where id =14785
union all
select * from selection_value where id =14766
union all
select * from selection_value where id =14768
union all
select * from selection_value where id =14796
union all
select * from selection_value where id =14771
union all
select * from selection_value where id =14772
union all
select * from selection_value where id =14774
union all
select * from selection_value where id =14776
union all
select * from selection_value where id =14764
union all
select * from selection_value where id =14780
union all
select * from selection_value where id =14781
union all
select * from selection_value where id =14767
union all
select * from selection_value where id =14784
union all
select * from selection_value where id =14785
union all
select * from selection_value where id =14786
union all
select * from selection_value where id =14788
union all
select * from selection_value where id =14789
union all
select * from selection_value where id =14790
union all
select * from selection_value where id =14791
union all
select * from selection_value where id =14785
union all
select * from selection_value where id =14772
union all
select * from selection_value where id =14792
union all
select * from selection_value where id =14796
union all
select * from selection_value where id =14785
union all
select * from selection_value where id =14797
union all
select * from selection_value where id =14798
union all
select * from selection_value where id =14799
union all
select * from selection_value where id =14800
union all
select * from selection_value where id =14802
union all
select * from selection_value where id =14778
union all
select * from selection_value where id =14803
union all
select * from selection_value where id =14758
union all
select * from selection_value where id =14765
union all
select * from selection_value where id =14762
union all
select * from selection_value where id =14781
union all
select * from selection_value where id =14785
union all
select * from selection_value where id =14786
union all
select * from selection_value where id =14808
union all
select * from selection_value where id =14793
union all
select * from selection_value where id =14805
union all
select * from selection_value where id =14807
union all
select * from selection_value where id =14808
于 2012-11-07T19:21:17.010 回答