首页 > 解决方案 > 如何检查一个集合是否是sql中另一个集合的子集

问题描述

在此处输入图像描述

文本到 DDL:

CREATE TABLE Recipe1(
    Ingredient varchar(10)
);
INSERT INTO Recipe1(Ingredient) VALUES('Flour'),('Egg'),('Sugar');
CREATE TABLE Recipe2(
    Ingredient varchar(10)
);
INSERT INTO Recipe2(Ingredient) VALUES('Egg'),('Sugar');

我想检查配方 2 是否是配方 1 的子集,并且我希望 SQL 返回一个布尔值,我该怎么做?我发现ORACLE支持一个叫做SUBSET的函数:

https://docs.oracle.com/cloud/latest/big-data-discovery-cloud/BDDEQ/reql_sets_subset.htm

但我找不到 SQL Server 甚至 MySQL 的等价物。我希望得到 SQL Server 的答案。

我们的讲师使用了这个代码: "Recipe 1" CONTAINS("Recipe 2")但它不起作用。另外,我没有使用或计划使用全文搜索,所以我需要一个解决方法。

标签: mysqlsqlsql-servercountrelational-division

解决方案


左连接也应该工作

Select count(*) ct 
from Recipe2 r2 
left join Recipe1 r1 on r2.Ingredient = r1.Ingredient
where r1.Ingredient is null

推荐阅读