LIKE 条件下的子查询

Subquery within LIKE condition

假设我有以下层次table:

+-------+----------+-------+-------------------------+-----------------+--------+
| ID    | ParentID |  Name |           Path          | InheritanceFlag | ToEdit |
+-------+----------+-------+-------------------------+-----------------+--------+
| 76938 | NULL     |   1   |         (76938)         | 1               | X      |
+-------+----------+-------+-------------------------+-----------------+--------+
| 76942 | 76938    |  1.1  |     (76938)\(76942)     | 1               | 1      |
+-------+----------+-------+-------------------------+-----------------+--------+
| 76952 | 76942    | 1.1.1 | (76938)\(76942)\(76952) | 0               | 0      |
+-------+----------+-------+-------------------------+-----------------+--------+
| 76961 | 76942    | 1.1.2 | (76938)\(76942)\(76961) | 1               | 1      |
+-------+----------+-------+-------------------------+-----------------+--------+
| 76943 | 76938    |  1.2  |     (76938)\(76943)     | 1               | 1      |
+-------+----------+-------+-------------------------+-----------------+--------+
| 76944 | 76938    |  1.3  |     (76938)\(76944)     | 0               | 0      |
+-------+----------+-------+-------------------------+-----------------+--------+
| 76946 | 76944    | 1.3.1 | (76938)\(76944)\(76946) | 1               | 0      |
+-------+----------+-------+-------------------------+-----------------+--------+
| 76947 | 76944    | 1.3.2 | (76938)\(76944)\(76947) | 0               | 0      |
+-------+----------+-------+-------------------------+-----------------+--------+
| 76948 | 76944    | 1.3.3 | (76938)\(76944)\(76948) | 1               | 0      |
+-------+----------+-------+-------------------------+-----------------+--------+
| 76945 | 76938    | 1.4   | (76938)\(76945)         | 1               | 1      |
+-------+----------+-------+-------------------------+-----------------+--------+

我的输入是 table 个 ID(它是我转换的 JSON 字符串),我想 select 这些 ID 及其后代(children 和children 的 children,等等)。例如,如果 ID 是 7694276946,它应该 return 我的行如下:

+-------+----------+-------+-------------------------+-----------------+--------+
| ID    | ParentID |  Name |           Path          | InheritanceFlag | ToEdit |
+-------+----------+-------+-------------------------+-----------------+--------+
| 76942 | 76938    |  1.1  |     (76938)\(76942)     | 1               | 1      |
+-------+----------+-------+-------------------------+-----------------+--------+
| 76952 | 76942    | 1.1.1 | (76938)\(76942)\(76952) | 0               | 0      |
+-------+----------+-------+-------------------------+-----------------+--------+
| 76961 | 76942    | 1.1.2 | (76938)\(76942)\(76961) | 1               | 1      |
+-------+----------+-------+-------------------------+-----------------+--------+
| 76946 | 76944    | 1.3.1 | (76938)\(76944)\(76946) | 1               | 0      |
+-------+----------+-------+-------------------------+-----------------+--------+

我怎样才能创建这个查询?我曾经用一个简单的 LIKEPath 来做到这一点,但由于它现在是一个列表,我不能使用它。

SELECT  
    [pkID]
FROM    
    [dbo].[t_Activites]
WHERE   
    [sFullPath] LIKE CONCAT('%(', @id, ')%')

对于那些对 CROSS APPLY 解决方案感兴趣的人,请看这里。我 运行 执行计划,它更有效率。

SELECT  A.pkID
FROM    t_Activites A
CROSS APPLY @Ids
JOIN    t_Activites A1
    ON  A1.pkID = s.id
WHERE   A.sFullPath LIKE CONCAT('%(', A1.pkID, ')%')

如果你有一个 table 变量,比如 @ids(id),你可以使用 exists:

SELECT a.pkID
FROM dbo.t_Activites a
WHERE EXISTS (SELECT 1 FROM @ids i WHERE a.sFullPath LIKE CONCAT('%(', i.id, ')%'))