Transact SQL - 加入使用

时间:2013-07-24 10:34:13

标签: sql-server

我有两个简单的SELECT语句: 第一个显示功能列表。

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..

我应该如何修改第三个SELECT语句来实现这一点,并结合功能和功能信息列表的结果?

2 个答案:

答案 0 :(得分:0)

正如评论所提到的,我需要另一个将功能链接到centre_translation_ids

的表格

首先获取所有feature / centre_translation varients

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

现在我们可以简单地执行LEFT JOIN

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

答案 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