2016-12-21 65 views
0

的另一範圍內我有一個像下面比較值範圍內值MYSQL

id_indicator id_threshold activation_begin_value activation_end_value 

    1   121     1      2 
    1   122     3      4 
    1   123     5      6 
    1   124     6      7 
    2   125     10      9 
    2   126     8      7 
    2   127     6      5 
    2   128     5      4 

表我怎麼能得到的細節,其中activation_begin_value和另一個閾值activation_end_value下跌。其中6個屬於id_threshold 123和124,與兩個id_threshold中的5個相同。查詢輸出如下:

id_indicator  id_threshold  activation_begin_value  activation_end_value 

    1   123     5      6 
    1   124     6      7 
    2   127     6      5 
    2   128     5      4 
+1

請參閱http://meta.stackoverflow.com/questions/333952/why-should-i-provide-an-mcve-for-what-seems-to-me-to-be-a-very-simple -sql-query – Strawberry

+0

我只想找出價值落入何處的thresold – Sumon

+0

我引用你對我以前的評論。 – Strawberry

回答

0

你的意思是類似的東西。

Select a.id_indicator  a.id_threshold  a.activation_begin_value  a.activation_end_value from table a 
where 1=1 
and a.activation_begin_value in (select activation_end_value from table) 
union 
Select b.id_indicator  b.id_threshold  b.activation_begin_value  b.activation_end_value from table b 
where 1=1 
and b.activation_end_value in (select activation_begin_value from table) 
+0

不給我輸出我在找, – Sumon

+0

不給我輸出我在找,每個指標有4個差異門檻,我需要找到具體的指標門檻值不落入差異門檻值,它可以是組通過指標 – Sumon

0

在我看來這是可能的自我加入和一些排序。礦山結果是你想要的一樣。

測試數據

CREATE TABLE Table1 
    (`id_indicator` INT, `id_threshold` INT, `activation_begin_value` INT, `activation_end_value` INT) 
; 

INSERT INTO Table1 
    (`id_indicator`, `id_threshold`, `activation_begin_value`, `activation_end_value`) 
VALUES 
    (1, 121, 1, 2), 
    (1, 122, 3, 4), 
    (1, 123, 5, 6), 
    (1, 124, 6, 7), 
    (2, 125, 10, 9), 
    (2, 126, 8, 7), 
    (2, 127, 6, 5), 
    (2, 128, 5, 4) 
; 

查詢

SELECT 
    table1.id_indicator 
, table1.id_threshold 
, table1.activation_begin_value 
, table1.activation_end_value 
FROM 
Table1 table1 
INNER JOIN 
Table1 table12 
ON 
table1.activation_begin_value = table12.activation_end_value 
ORDER BY 
    table1.id_indicator ASC 
    , table1.id_threshold ASC 

結果

id_indicator id_threshold activation_begin_value activation_end_value 
------------ ------------ ---------------------- ---------------------- 
      1   123      5      6 
      1   124      6      7 
      2   127      6      5 
      2   128      5      4 

觀看演示http://sqlfiddle.com/#!9/d18f2/2

+0

對不起,我有點困惑,你使用4表 – Sumon

+0

我的表名是閾值,我必須使用代碼像FROM閾值1,閾值2內部加入閾值3,閾值4 – Sumon

+0

對不起,我現在得到你的觀點你使用別名作爲表1 – Sumon