2013-03-07 72 views
0

我有2個表如何在數據庫中查詢此信息?

表1

Positon | Name | Nickname | Address 
------------------------------------ 
    1 | 2 | 3  | 4 
    5 | 6 |   | 7 
    5 | 6 |   | 8 

表2

Detail_seq | Details 
-------------------- 
    1  | manager 
    2  | Jimmy 
    3  | Jim 
    4  | Chicago 
    5  | Supervisor 
    6  | Mike 
    7  | Vancouver 
    8  | New York 

和輸出應該是這個樣子什麼的

Position: Manager 
Name: Jimmy 
Nickname: Jim 
Address: Chicago 

Position: Supervisor 
Name: Mike 
Nickname: 
Address: Vancouver and New York 

隨時如果提問不清楚。

在此先感謝

+0

爲什麼把你的詳細信息在一張獨立的桌子裏,把它們放在你的第一張桌子上? – LukeHennerley 2013-03-07 09:40:28

+0

你的輸出真的不清楚 – DevelopmentIsMyPassion 2013-03-07 09:41:08

+0

@TimSchmelter你說得對,但至少我們可以理解不清楚的問題 – 2013-03-07 09:46:00

回答

1

我想這是你想要什麼:

SELECT Position=manager.details, 
     Name=Name.details, 
     Nickname=Nickname.details, 
     Address=Address.details 
FROM table1 t1 
     LEFT OUTER JOIN table2 manager 
        ON t1.positon = manager.detail_seq 
     LEFT OUTER JOIN table2 name 
        ON t1.name = name.detail_seq 
     LEFT OUTER JOIN table2 Nickname 
        ON t1.nickname = Nickname.detail_seq 
     LEFT OUTER JOIN table2 Address 
        ON t1.address = Address.detail_seq 

DEMO

+0

感謝您的幫助。真的很感激它。榮譽! – detweiller 2013-03-08 03:37:02

1

以這種方式存儲數據有點極端。我可以爲您提供將數據類別存儲在不同的表格中。但如果你堅持,這應該工作

SELECT d1.details, d2.details, d3.details, d4.details 
FROM main m, details d1, details d2, details d3, details d4 
WHERE m.position = d1.detail_seq 
AND m.name  = d2.detail_seq 
AND m.nickname = d3.detail_seq 
AND m.adress = d4.detail_seq 
+0

不,它不應該,兩行中的暱稱是空的,所以你需要一個'LEFT JOIN'而不是'INNER JOIN'。 – 2013-03-07 10:00:46

+0

你是對的;) – Doruk 2013-03-07 10:03:40

1

你需要加入細節表一次每一個細節:

SELECT pos.Details, name.details, nick.details, adress.details 
    FROM table1 t1 
    LEFT JOIN table2 pos  ON pos.detail_seq = t1.position 
    LEFT JOIN table2 name ON name.detail_seq = t1.name 
    LEFT JOIN table2 nick ON nick.detail_seq = t1.nickname 
    LEFT JOIN table2 address ON address.detail_seq = t1.address 
0
select (select details from t2 where t2.detail_seq=t1.position) position, 
     (select details from t2 where t2.detail_seq=t1.name) Name, 
     (select details from t2 where t2.det_seq=t1.nickname) nickname, 
     (select details from t2 where t2.det_seq=t1.address) address 
from t1 ; 
+0

謝謝你們..你們所有的答案都給了我這個想法。 – detweiller 2013-03-08 02:10:42

相關問題