2012-11-07 68 views
1

我有這個疑問...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按這個順序?

這可能嗎?

+0

僅僅是您的數據集還是您需要從其他表中提取數據? – nickhar

回答

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 
+0

會保存OP所需的順序嗎? – Ankur

+0

看我的編輯。我沒有意識到訂單沒有按升序排列:) – SchmitzIT

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; 
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) 
+0

這不會給OP所需的訂單 – Ankur

+0

剛剛看到! – nickhar

2

使用帶逗號分隔值的IN子句。

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

這不會給OP所需的訂單 – Ankur

+0

是的,剛剛看到原來的帖子。感謝您的澄清。具有自定義排序順序的派生表將可用。 – Prakash

+0

是的,它會通過@RichardTheKiwi回答http://stackoverflow.com/a/13276289/662250 – Ankur

-1

你的問題就粘貼到記事本++,就以下搜索中的正則表達式模式&替代對象:

  • \ r - > ''
  • \ n - > \ nunion所有\ 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 
相關問題