mysql - 如何检查一个集合是否是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")
但它不起作用。另外,我没有使用或计划使用全文搜索,所以我需要一个解决方法。
解决方案
左连接也应该工作
Select count(*) ct
from Recipe2 r2
left join Recipe1 r1 on r2.Ingredient = r1.Ingredient
where r1.Ingredient is null
推荐阅读
- c# - 在 collection 上查找特定项目
- batch-file - 批处理文件将文本文件的最后 n 行复制到新的文本文件中
- outlook-restapi - Outlook REST API 正在为消息中的事件返回 404
- mysql - 将 MySQL 数据作为 OBJECT 而不是 ARRAY (Knex) 返回
- java - Google Play 游戏排行榜不刷新
- mongodb - 错误:exec:“/usr/local/bin/docker-entrypoint.sh”:stat /usr/local/bin/docker-entrypoint.sh:权限被拒绝
- react-native - 无法运行项目
- bash - 等待 Bash IO 重定向中的 subshell
- regex - Smarty:为什么 regex_replace 在标签中使用类时不起作用?
- python-3.x - 无法使用 boto3 创建 s3 存储桶