2014-11-09 179 views
0

我有三個錶鏈,酒店和殼牌。酒店錶鏈接鏈表和外殼表與兩個表的鏈接。SQL查詢不能正常工作

表結構:---

// 2級

ChainID | ChainName 

酒店 // 3級

HotelID | HotelName | ChainID 

殼牌

ShellID | ShellName | Chain_Hotel_ID | Level 

在殼牌表,Chain_Hotel_ID柱保持(連鎖和酒店)表ChainID和HotelID和等級欄中按住INT值2和3。鏈認爲是2和酒店3

從這些表我想

ShellID | ShellName | ChainName 

我編寫一個查詢爲,但它不工作,我想

select 
    _gs.ShellID, _gs.ShellName, _gc.ChainName 
from 
    Shell _gs, Chain _gc 
where 
    _gc.ChainID in (select _lh.ChainID 
        from Shell _ls,Hotel _lh 
        where _lh.HotelID = _ls.Chain_Hotel_ID and _ls.Level = 3) 
    and _gs.Level = 3 

樣品表數據

ChainID | ChainName 
    1  | Sarovar 
    2  | Taj Group 
    3  | Oberoi 

酒店

HotelID | HotelName | ChainID 
    1  | RadhaRegent |  1 
    2  | SarovarPortic |  1 
    3  | LaxmiVilas |  1 
    4  | Taj Mumbai |  2 
    5  | Taj Delhi  |  2 
    6  | Oberoi Delhi |  3 

殼牌

ShellID | ShellName | Chain_Hotel_ID | Level 
    1  | Shell1  |  1    |  2 
    2  | Shell2  |  1    |  2 
    3  | Shell3  |  2    |  3 
    4  | Shell4  |  3    |  2 
    5  | Shell5  |  2    |  3 
    6  | Shell6  |  3    |  3 
    7  | Shell7  |  4    |  3 

輸出

ShellID | ShellName | ChainName  
    3  | Shell3  |  Sarovar   
    5  | Shell5  |  Sarovar   
    6  | Shell6  |  Sarovar   
    7  | Shell7  |  Taj Group 
    3  | Shell3  |  Sarovar   
    5  | Shell5  |  Sarovar   
    6  | Shell6  |  Sarovar   
    7  | Shell7  |  Taj Group 

我[R反覆數據

+3

請出示一些示例數據和預期輸出與sqlfiddle.com – 2014-11-09 08:31:23

+4

[不良習慣踢:使用OLD- style JOINs](http://sqlblog.com/blogs/aaron_bertrand/archive/2009/10/08/bad-habits-to-kick-using-old-style-joins.aspx) - 舊式*逗號 - 在ANSI - ** 92 ** SQL標準(**超過20年**之前)中,* *樣式的分隔列表被替換爲* proper * ANSI'JOIN'語法,不鼓勵 – 2014-11-09 08:31:26

+0

@安傑爾你的期望輸出是什麼? – 2014-11-09 08:51:46

回答

2

這是你所需要的?

IF LEVEL=3 
BEGIN 
SELECT DISTINCT ShellID, ShellName, ChainName 
FROM HOTEL JOIN SHELL 
ON HOTEL.CHAINID=SHELL.CHAIN_HOTEL_ID 
JOIN 
CHAIN ON CHAIN.CHAINID=HOTEL.CHAINID 
END 
0

您只需按給定順序連接表:Shell - > Hotel - > Chain。

下面是代碼示例:

SELECT 
    s.Id AS ShellID, s.Name AS ShellName, c.Name AS ChainName 
FROM #shell s 
INNER JOIN #hotel h ON h.Id = s.HotelId 
INNER JOIN #chain c ON c.Id = h.ChainID 
WHERE s.Level = 3 

全面上市,以測試:

CREATE TABLE #chain (Id INT, Name varchar(50)) 
GO 
INSERT INTO #chain VALUES (1, 'Sarovar'), (2, 'Taj Group'), (3, 'Oberoi') 
GO 

CREATE TABLE #hotel (Id INT, Name varchar(50), ChainId INT) 
GO 
INSERT INTO #hotel VALUES 
    (1, 'RadhaRegent' , 1), 
    (2, 'SarovarPortic' , 1), 
    (3, 'LaxmiVilas' , 1), 
    (4, 'Taj Mumbai' , 2), 
    (5, 'Taj Delhi'  , 2), 
    (6, 'Oberoi Delhi' , 3) 
GO 

CREATE TABLE #shell (Id INT, Name varchar(50), HotelId INT, Level INT) 
GO 
INSERT INTO #shell VALUES 
    (1, 'Shell1', 1, 2), 
    (2, 'Shell2', 1, 2), 
    (3, 'Shell3', 2, 3), 
    (4, 'Shell4', 3, 2), 
    (5, 'Shell5', 2, 3), 
    (6, 'Shell6', 3, 3), 
    (7, 'Shell7', 4, 3) 
GO 

SELECT 
    s.Id AS ShellID, s.Name AS ShellName, c.Name AS ChainName 
FROM #shell s 
INNER JOIN #hotel h ON h.Id = s.HotelId 
INNER JOIN #chain c ON c.Id = h.ChainID 
WHERE s.Level = 3 

DROP TABLE #chain; 
GO 
DROP TABLE #hotel; 
GO 
DROP TABLE #shell; 
GO