1

我有两个简单的 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 语句来实现这一点并结合功能和功能信息列表的结果?

4

2 回答 2

0

正如评论所暗示的,我需要另一个将功能链接到 center_translation_ids 的表

首先获取所有的特征/center_translation 变体

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
于 2013-07-24T13:28:42.513 回答
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
于 2013-07-24T13:49:14.577 回答