有条件的左连接

时间:2014-02-03 08:59:35

标签: tsql

我有两个表:ItemCategories和Items。 ItemCategories列出项目的类别,并包含语言相关的文本字符串。 CultureCode列确定文本属于哪种语言。

ItemCategories

ID  Name            CultureCode
1   Food            'en-us'
2   Clothing        'en-us'
3   Computers       'en-us'
4   Books           'en-us'
1   Essen           'de-de'
2   Kleidung        'de-de'
3   Rechner         'de-de'
4   Bücher          'de-de'

产品

ID  CategoryID1 CategoryID2
4   1           NULL
5   NULL        2
6   3           4
7   NULL        NULL

Items表列出了每个项目,CategoryID1和CategoryID2下的值引用了CategoryItems中的ID字段。 CategoryID1和CategoryID2的值都可以为null,既可以设置为ID,也可以只设置为其中一个ID值。

当CultureCode设置为特定语言时,我需要创建一个SQL(T-SQL),它返回Items中的ID以及ItemCategories中的对应Name。因此,如果我的CultureCode设置为'en-us',结果应为:

ID  CategoryID1 CategoryID2  CategoryName1  CategoryName2
4   1           NULL         Food           Null
5   NULL        2            null           Clothing
6   3           4            Computers      Books
7   NULL        NULL         null           null

这是我到目前为止所做的:

Select I.*
From Items I
Left Join ItemCategories IC On (IC.ID = I.CategoryID1) Or (IC.ID = I.CategoryID2)
where (IC.CultureCode = 'en-us') Or (IC.CultureCode is null)

但这并没有给我我想要的结果。

3 个答案:

答案 0 :(得分:1)

听起来您需要将LEFT JOIN分成两个单独的LEFT JOIN

SELECT I.ID, I.CategoryID1, I.CategoryID2, CategoryName1 = IC1.Name, CategoryName2 = IC2.Name
    FROM Items I
        LEFT JOIN ItemCategories IC1 ON IC1.ID = I.CategoryID1 AND IC1.CultureCode = 'en-us'
        LEFT JOIN ItemCategories IC2 ON IC2.ID = I.CategoryID2 AND IC2.CultureCode = 'en-us';

答案 1 :(得分:0)

尝试一下,让我知道你的结果/评论。

 CREATE TABLE ItemCategories
(
ID  INT
,Name VARCHAR(50)
,CultureCode    VARCHAR(5)

)
GO
INSERT INTO ItemCategories VALUES(1  ,'Food '      ,     'en-us')
INSERT INTO ItemCategories VALUES(2  ,'Clothing'   ,     'en-us')
INSERT INTO ItemCategories VALUES(3  ,'Computers'  ,     'en-us')
INSERT INTO ItemCategories VALUES(4  ,'Books'      ,     'en-us')
INSERT INTO ItemCategories VALUES(1  ,'Essen'      ,     'de-de')
INSERT INTO ItemCategories VALUES(2  ,'Kleidung'   ,     'de-de')
INSERT INTO ItemCategories VALUES(3  ,'Rechner'    ,    'de-de' )
INSERT INTO ItemCategories VALUES(4  ,'Bücher '    ,     'de-de')


CREATE TABLE Items
(
ID  INT
,CategoryID1 INT
,CategoryID2    INT

)
GO

INSERT INTO Items VALUES (4  , 1       ,    NULL)
INSERT INTO Items VALUES (5  , NULL    ,    2   )
INSERT INTO Items VALUES (6  , 3       ,    4   )
INSERT INTO Items VALUES (7  , NULL    ,    NULL)

Select I.*,IC.Name,IT.Name
From Items I 
    left join ItemCategories IC ON (IC.ID = I.CategoryID1) AND IC.CultureCode = 'en-us'
    left join ItemCategories IT   On (It.ID = I.CategoryID2) AND IT.CultureCode = 'en-us'

答案 2 :(得分:0)

您可以通过两种方式实现:

使用子查询:

Select 
    I.ID,
    (SELECT TOP 1 ID FROM ItemCategories IC WHERE IC.ID=I.CategoryID1 AND IC.CultureCode = 'en-us') AS CategoryID1,
    (SELECT TOP 1 ID FROM ItemCategories IC WHERE IC.ID=I.CategoryID2 AND IC.CultureCode = 'en-us') AS CategoryID2,
    (SELECT TOP 1 Name FROM ItemCategories IC WHERE IC.ID=I.CategoryID1 AND IC.CultureCode = 'en-us') AS CategoryName1,
    (SELECT TOP 1 Name FROM ItemCategories IC WHERE IC.ID=I.CategoryID2 AND IC.CultureCode = 'en-us') AS CategoryName2
From Items I

或者,将查询拆分为2个连接

SELECT DISTINCT I.*, IC1.Name AS CategoryName1, IC2.Name AS CategoryName2
FROM Items I
LEFT JOIN ItemCategories IC1 ON I.CategoryID1=IC1.ID
LEFT JOIN ItemCategories IC2 ON I.CategoryID2=IC2.ID
WHERE (IC1.CultureCode IS NULL OR IC1.CultureCode = 'en-us') AND (IC2.CultureCode IS NULL OR IC2.CultureCode = 'en-us');