这是 SQL Server 代码。
假设您有一个包含三列的表。第 1 列命名为 Monster,第 2 列命名为 Level,第 3 列命名为 BodyType。Level表示怪物的强大程度,BodyType表示它的身体类型。
我的架构:
CREATE TABLE YourTable
([Monster] nvarchar(max), [Level] int, [BodyType] nvarchar(max))
;
INSERT INTO YourTable
([Monster], [Level], [BodyType])
VALUES
('Small Beast', 300, 'Scaly'),
('Large Beast', 700, 'Slimy'),
('Small Dragon', 350, 'Fiery'),
('Large Dragon', 800, 'Slimy')
;
我有一个 sql 命令来查找怪物的所有可能组合。它使用递归 cte,因为表中的怪物数量可能会波动(所以我可以稍后添加更多怪物)。该命令还获取正在组合的怪物等级的总和值。该命令还只输出低于某个总和值的怪物组合。在此示例中,总和值为 1500。到目前为止,一切都按其应有的方式工作。
我的 sql 命令:
;WITH cte AS (
SELECT Monster,
[Level],
BodyType,
1 as l
FROM YourTable
UNION ALL
SELECT c1.Monster+','+c2.Monster,
c1.[Level]+c2.[Level],
c1.BodyType+','+c2.BodyType,
c1.l+1
FROM cte c1
CROSS JOIN YourTable c2
WHERE c1.Monster NOT LIKE '%'+c2.Monster+'%'
)
SELECT *
FROM cte
WHERE cte.Level < 1500
ORDER BY l
OPTION (MAXRECURSION 0)
和正确的输出:
1 Small Beast 300 Scaly 1
2 Large Beast 700 Slimy 1
3 Small Dragon 350 Fiery 1
4 Large Dragon 800 Slimy 1
5 Large Dragon,Small Beast 1100 Slimy,Scaly 2
6 Large Dragon,Small Dragon 1150 Slimy,Fiery 2
7 Small Dragon,Small Beast 650 Fiery,Scaly 2
8 Small Dragon,Large Beast 1050 Fiery,Slimy 2
9 Small Dragon,Large Dragon 1150 Fiery,Slimy 2
10 Large Beast,Small Beast 1000 Slimy,Scaly 2
11 Large Beast,Small Dragon 1050 Slimy,Fiery 2
12 Small Beast,Large Beast 1000 Scaly,Slimy 2
13 Small Beast,Small Dragon 650 Scaly,Fiery 2
14 Small Beast,Large Dragon 1100 Scaly,Slimy 2
15 Small Beast,Large Dragon,Small Dragon 1450 Scaly,Slimy,Fiery 3
16 Small Beast,Small Dragon,Large Beast 1350 Scaly,Fiery,Slimy 3
17 Small Beast,Small Dragon,Large Dragon 1450 Scaly,Fiery,Slimy 3
18 Small Beast,Large Beast,Small Dragon 1350 Scaly,Slimy,Fiery 3
19 Large Beast,Small Dragon,Small Beast 1350 Slimy,Fiery,Scaly 3
20 Large Beast,Small Beast,Small Dragon 1350 Slimy,Scaly,Fiery 3
21 Small Dragon,Large Dragon,Small Beast 1450 Fiery,Slimy,Scaly 3
22 Small Dragon,Large Beast,Small Beast 1350 Fiery,Slimy,Scaly 3
23 Small Dragon,Small Beast,Large Beast 1350 Fiery,Scaly,Slimy 3
24 Small Dragon,Small Beast,Large Dragon 1450 Fiery,Scaly,Slimy 3
25 Large Dragon,Small Dragon,Small Beast 1450 Slimy,Fiery,Scaly 3
26 Large Dragon,Small Beast,Small Dragon 1450 Slimy,Scaly,Fiery 3
我遇到的问题是当我添加 Where 子句以仅带回不属于某种体型(BodyType 列)的怪物时。修改上面的代码部分以实现此目的是:
;WITH cte AS (
SELECT Monster,
[Level],
BodyType,
1 as l
FROM YourTable
WHERE BodyType NOT LIKE 'Fiery' AND BodyType NOT LIKE 'Slimy'
UNION ALL
输出变为以下错误,因为它仍然包含 Slimy 和 Fiery 的身体类型:
Monster Level BodyType l
1 Small Beast 300 Scaly 1
2 Small Beast,Large Beast 1000 Scaly,Slimy 2
3 Small Beast,Small Dragon 650 Scaly,Fiery 2
4 Small Beast,Large Dragon 1100 Scaly,Slimy 2
5 Small Beast,Large Dragon,Small Dragon 1450 Scaly,Slimy,Fiery 3
6 Small Beast,Small Dragon,Large Beast 1350 Scaly,Fiery,Slimy 3
7 Small Beast,Small Dragon,Large Dragon 1450 Scaly,Fiery,Slimy 3
8 Small Beast,Large Beast,Small Dragon 1350 Scaly,Slimy,Fiery 3
输出似乎部分工作,因为 Large Beast 是 Slimy 并且它第一次忽略了它,但我怀疑它在通过 BodyType 的级别时忽略了 NOT LIKE 子句,这就是为什么它在随后的发现中没有忽略 Large Beast 的原因。