簡體   English   中英

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

[英]SQL query - whats the best way to do this

我有這個查詢...

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

這可能嗎?

使用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

您可以使派生表的在一個列中匹配 ,而在另一列中它們的順序匹配 -將其聯接到表中並通過排序列進行排序。

  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;

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

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

您的問題並不十分清楚,但也許:

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)

只需將您的問題粘貼到notepad ++中,在正則表達式模式下執行以下搜索和替換操作即可:

  • \\ r->''
  • \\ n-> \\ nunion all \\ nselect * from 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM