2017-04-21 79 views
2

我有數據列表和下面的表格。
數據:{ABC111,ABC112,ABC113,111111,111112,111113}
查詢多對多關係數據庫表

Column 1 Column 2 
ABC111 ABC115 
ABC115 111333 
111111 ABC112 
111111 111112 
ABC123 111113 
111113 ABC113 

我的結果應該是像下面
結果:{ABC115 ,111333,111113,ABC113,ABC123,ABC112,111112}

說明
數據 - ABC111與ABC115相關聯,ABC115也與111333相關聯。因此結果是ABC115,111333。 類似地,ABC113與關聯到ABC123的111113相關聯。 結果 = 111113,ABC123。

是否可以使用查詢來實現上述操作。尋找準備好的陳述,我將通過上面提到的數據。

+1

'mysql'或'oracle'? –

+0

尋找mysql準備的語句 – Roopashree

+0

查看https://meta.stackoverflow.com/questions/333952/why-should-i-provide-an-mcve-for-what-seems-to-me-to-bea-a- very-simple-sql-query – Strawberry

回答

0

試試這個: -

/*CREATING RECORDS FOR THE MAIN TABLE*/ 

CREATE TABLE TABLES(Column1 text, Column2 text); 

INSERT INTO TABLES VALUES('ABC111','ABC115'); 
INSERT INTO TABLES VALUES('ABC115','111333'); 
INSERT INTO TABLES VALUES('111111','ABC112'); 
INSERT INTO TABLES VALUES('111111','111112'); 
INSERT INTO TABLES VALUES('ABC123','111113'); 
INSERT INTO TABLES VALUES('111113','ABC113'); 
COMMIT; 


/*CREATE LIST TO BE SUPPLIED*/ 

CREATE TABLE LIST(Column1 text); 

INSERT INTO LIST VALUES('ABC111'); 
INSERT INTO LIST VALUES('ABC112'); 
INSERT INTO LIST VALUES('ABC113'); 
INSERT INTO LIST VALUES('111111'); 
INSERT INTO LIST VALUES('111112'); 
INSERT INTO LIST VALUES('111113'); 
COMMIT; 


/*CODE TO GET THE RESULT*/ 

SELECT CONCAT('{',GROUP_CONCAT(RESULT),'}') AS RESULT FROM 
(
SELECT DISTINCT COLUMN1 AS RESULT FROM 
    (
      Select a.column2 as column1,b.column2 
      from 
      (
      Select * from 
      tables 
      where column1 in (SELECT COLUMN1 FROM LIST) 
      ) a 
      inner join 
      tables b 
      on a.column2=b.column1 

      UNION ALL 

      Select b.column2 as column1,b.column1 as Column2 
      from 
      (
      Select * from 
      tables a 
      where column2 in (SELECT COLUMN1 FROM LIST) 
      ) a 
      inner join 
      tables b 
      on a.column1=b.column2 
) a 

UNION ALL 

SELECT DISTINCT COLUMN2 AS RESULT FROM 
(
      Select a.column2 as column1,b.column2 
      from 
      (
      Select * from 
      tables 
      where column1 in (SELECT COLUMN1 FROM LIST) 
      ) a 
      inner join 
      tables b 
      on a.column2=b.column1 

      UNION ALL 

      Select b.column2 as column1,b.column1 as Column2 
      from 
      (
      Select * from 
      tables a 
      where column2 in (SELECT COLUMN1 FROM LIST) 
      ) a 
      inner join 
      tables b 
      on a.column1=b.column2 
) a 

UNION ALL 

SELECT DISTINCT COLUMN2 AS RESULT FROM 
(

     Select distinct a.Column1, a.column2 
     from 
     (
     Select * from 
     tables a 
     where column2 in (SELECT COLUMN1 FROM LIST) 
     ) a 
     inner join 
     (
     Select * from 
     tables a 
     where column2 in (SELECT COLUMN1 FROM LIST) 
     ) b 
     on a.column1=b.column1 
     where a.column2 not in 
    (
    Select column1 from 
     (
      Select b.column2 as column1,b.column1 as Column2 
      from 
      (
      Select * from 
      tables a 
      where column2 in (SELECT COLUMN1 FROM LIST) 
      ) a 
      inner join 
      tables b 
      on a.column1=b.column2 
     ) a 
    )  
) a 
) a; 

希望這有助於:-)

+0

這真的很有用@ India.Rocket我希望你使用遞歸查詢。我只想確認這是否是一種有效的方法。有沒有其他方式有效地實施它。我們可以使用java和sql結合來實現這個嗎? – Roopashree

+0

我不知道Java,我不認爲它可以使用遞歸查詢來完成。我想了很多。但是,如果有人能夠這樣回答,那將會很棒。反正一個非常好的問題。 –