简体   繁体   English

在SQL Server中如何使用联接?

[英]How can I use join in case in SQL Server?

How can I convert "in" to inner join in case: 在以下情况下,如何将“输入”转换为内部联接:

SELECT 
    m.*, 
    atypcode_type = CASE 
                       WHEN m.[ATYPCODE] IS NOT NULL 
                            AND m.[ATYPCODE] IN (SELECT lastlevel 
                                                 FROM [dbo].[tbl2] 
                                                 WHERE ','+'2921'+',' LIKE '%,' + CAST([ParentId] AS VARCHAR(10)) + ',%') 
                          THEN 100
                       WHEN m.[ATYPCODE] IS NOT NULL 
                            AND m.[ATYPCODE] IN (SELECT lastlevel 
                                                 FROM [dbo].[tbl2]  
                                                 WHERE ',' + '3366' + ',' LIKE '%,' + CAST([ParentId] AS VARCHAR(10)) + ',%') 
                          THEN 101
                       ....
                    END
FROM 
    [tbl1] m
INNER JOIN
    [tbl2] ncg ON m.ATYPCODE = ncg.lastlevel 

How can I convert this code 如何转换此代码

m.[ATYPCODE] IN (SELECT lastlevel FROM [dbo].[tbl2] 
                 WHERE ','+'2921'+',' like '%,'+cast([ParentId] AS varchar(10))+',%')

to an inner join? 参加内部联接?

Result is : 结果是:

atypcode    acurrcode   tarikh  atypcode_type
    2741    IRR 1397/06/31  109
    2941    IRR 1397/06/31  109
    3371    IRR 1397/06/31  101
    3381    IRR 1397/06/31  101
    3671    IRR 1397/06/31  101
    3381    IRR 1397/06/31  101
    4371    IRR 1397/06/31  101
    3971    IRR 1397/06/31  101
    3571    IRR 1397/06/31  101
    3771    IRR 1397/06/31  101

Create col atypcode_type with case, but this code it's too slow 使用大小写创建col atypcode_type ,但是此代码太慢

This answers the question that you have asked: 这回答了您提出的问题:

SELECT m.*,
       (CASE WHEN m.[ATYPCODE] IS NOT NULL AND
                  t2_2921.lastlevel IS NOT NULL
             THEN 100
             WHEN m.[ATYPCODE] IS NOT NULL AND
                  t2_3366.lastlevel IS NOT NULL 
             THEN 101
             ....
        END) as atypcode_type
FROM [tbl1] m INNER JOIN
     [tbl2] ncg
     ON m.ATYPCODE = ncg.lastlevel LEFT JOIN
     tbl2 t2_2921
     ON m.ATYPCODE = t2_2921.lastlevel AND
        ',' + '2921' + ',' LIKE '%,' + CAST(?.ParentId AS VARCHAR(10)) + ',%') LEFT JOIN
     tbl2 t2_3366
     ON m.ATYPCODE = t2_3366.lastlevel AND
        ',' + '3366' + ',' LIKE '%,' + CAST(?.ParentId AS VARCHAR(10)) + ',%')

It does make the assumption that your delimited list has no duplicates. 它确实假设您的定界列表没有重复项。

The ? ? is for the table alias to identify where ParentId comes from. 用于表别名以标识ParentId来源。

I have a few editorial comments. 我有一些社论评论。 First, I find it hard to imagine that this code does anything useful. 首先,我很难想象这段代码会做任何有用的事情。 It seems to be mixing levels and parents in a rather convoluted way. 它似乎以一种相当复杂的方式混合了水平和父母。 You haven't explained your data structure so this might be what you want. 您尚未解释数据结构,因此可能正是您想要的。

Second, there is no performance gain from this. 第二,没有任何性能提升。 You don't specify why you want to switch to a JOIN . 您没有指定为什么要切换到JOIN If that is the reason why, this has little benefit. 如果那是原因,那么这几乎没有好处。

And that is related to the third reason. 这与第三个原因有关。 Do not store multiple values as comma-delimited lists. 不要将多个值存储为以逗号分隔的列表。 That is not the SQL way to store data. 那不是SQL存储数据的方式。 Relational databases have this great data structure to store lists. 关系数据库具有很好的数据结构来存储列表。 It is called a table , not a string . 它称为 ,而不是字符串

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM