2013-07-24 5 views
1

두 개의 간단한 SELECT 문이 있습니다. 첫 번째는 기능 목록을 표시합니다.Transact SQL - 사용할 가입

SELECT * FROM Features 

id name 
-- ---- 
1 24 Hour Access 
2 24 hour CCTV monitoring 
3 Airport location 
4 Break-Out Areas 
5 Business Lounge 
6 Business park location 
snip.. 

두 번째 문은

SELECT 
    * 
FROM 
    #SmartFeaturesToUpdate new_features 
ORDER BY 
    new_features.centre_translation_id, 
    new_features.feature_id, 
    new_features.feature_selected 

feature_id centre_translation_id feature_selected 
---------- --------------------- ---------------- 
1   1     1 
2   1     1 
5   1     1 
10   1     1 
11   1     1 
snip.. 

내가보고 싶은 것은 센터 번역에 의해 모든 기능이다

을 변경 기능 정보의 목록을 보여줍니다. 테이블을 결합 저를 제공합니다

SELECT 
    * 
FROM 
    #SmartFeaturesToUpdate new_features 
    LEFT JOIN Feature feature ON feature.id = new_features.feature_id 
ORDER BY 
    new_features.centre_translation_id, 
    new_features.feature_id, 
    new_features.feature_selected 

feature_id centre_translation_id feature_selected id name 
---------- --------------------- ---------------- -- ---- 
1   1     1    1 24 Hour Access 
2   1     1    2 24 hour CCTV monitoring 
5   1     1    5 Business Lounge 
10   1     1    10 Double Glazing 
11   1     1    11 Elevator 
snip.. 

위없는 결과 기능 ID의 3, 4, 그들은 두 번째 목록에없는 때문이다. 하지만 내가 필요로하는 결과는 다음과 같습니다

feature_id centre_translation_id feature_selected id name 
---------- --------------------- ---------------- -- ---- 
1   1     1    1 24 Hour Access 
2   1     1    2 24 hour CCTV monitoring 
3   1     1    3 Airport Location 
4   1     1    4 Break-Out Area 
5   1     1    5 Business Lounge 
snip.. 

어떻게이 acheive과 기능 모두에서 결과를 결합하는 세 번째 SELECT 문을 수정 및 정보 목록을 특징으로한다? 코멘트가 언급으로

+1

차이점과 달성하고자하는 단어로 설명해 주시겠습니까? 예제는 훌륭하지만 몇 단어 migth 도움이됩니다. – Eggplant

+3

"Break-Out Area"에 대한 centre_translation_id 및 feature_selected 열의 값은 어디서 얻습니까? – openshac

+0

업데이트 되었습니까? – NiceYellowEgg

답변

0

, 나는

먼저 얻는 기능/centre_translation의 모든 이제 우리는 단순히 LEFT JOIN을 수행 할 수 있습니다

SELECT 
    [centre_translation_id] = centre_translation.id, 
    feature.id, 
    feature.name 
    INTO #AllTheFeatures 
FROM 
    CentreTranslation centre_translation 
    CROSS JOIN Feature feature 
ORDER BY 
    centre_translation.id, 
    feature.id 

을 varients centre_translation_ids에 기능을 연결 다른 테이블이 필요

SELECT 
    all_features.centre_translation_id, 
    all_features.id, 
    all_features.name, 
    smart_features.feature_selected 
FROM 
    #AllTheFeatures all_features 
    LEFT JOIN #SmartFeaturesToUpdate smart_features ON smart_features.centre_translation_id = all_features.centre_translation_id AND 
                 smart_features.feature_id = all_features.id 
ORDER BY 
    all_features.centre_translation_id, 
    all_features.id 

결과는 다음과 같습니다.

centre_translation_id id name     feature_selected 
--------------------- -- ----     ---------------- 
1      1 24 Hour Access   1 
1      2 24 hour CCTV monitoring 1 
1      3 Airport location  NULL 
1      4 Break-Out Areas   NULL 
1      5 Business Lounge   1 
0

왜 하나의 쿼리에 넣지 않으시겠습니까?

SELECT 
    centre_translation.id AS centre_translation_id, 
    feature.id, 
    feature.name, 
    smart_features.feature_selected 
FROM 
    CentreTranslation centre_translation 
CROSS JOIN Feature feature 
LEFT JOIN #SmartFeaturesToUpdate smart_features 
    ON smart_features.centre_translation_id = all_features.centre_translation_id 
    AND smart_features.feature_id = all_features.id 
ORDER BY 
    centre_translation.centre_translation_id, 
    feature.id