SQL查询 - 最好的方法是什么

时间:2012-11-07 18:53:31

标签: sql

我有这个问题......

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数据的最佳方法是什么?

这可能吗?

5 个答案:

答案 0 :(得分: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

答案 1 :(得分: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;

答案 2 :(得分:2)

IN子句与逗号分隔值一起使用。

select * from selection_value where id IN (14702,14703, so on) ORDER BY ID

答案 3 :(得分: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)

答案 4 :(得分:-1)

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

  • \ r - > ''
  • \ n - > \ nnunion all \ n选择*来自selection_value,其中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