匹配 MySQL 中的每个值至少一个

发布于 2024-10-16 16:53:13 字数 1017 浏览 2 评论 0原文

我有 2 个表,我想从一个表中选择条目,其中第二个表中有用于项目列表的条目。

例如:

条目

id=1 requirements=(1,2,3)

id=2 requirements=(1,3,5)

提交

id=1 entry=1 requirement=1

id=2 entry=2 requirement=2

id=3 entry=3 requirement=3

会选择条目 id=1,但不会条目 id=2(因此 WHERE IN不做这项工作)

*编辑实际数据集:

INSERT INTO submission (entry_id, requirement_id, submission_id)价值观 (17,1,1), (43,1,2), (57,0,3), (57,0,4), (1,1,5), (26,1,6), (40,1,7), (40,1,8), (40,1,9), (40,1,10), (85,1,11), (94,1,12), (114, 0, 13), (32,1,14), (34, 0, 15);

INSERT INTO entry (entry_id, category_id) 值 (1, 2), (2, 1), (3, 1), (4, 1), (5, 2), (6, 1), (7, 1), (8, 1), (9, 1), (10, 1), (11, 1), (12, 1), (13, 1), (14, 1), (15, 1);

插入类别category_id要求)值 (1, '1,2,3'), (2, '1,2,4,5,6'), (3, '1,2,4,5,6');

I've got 2 tables, I want to select entries from one table, where there are entries in the second table for a list of items.

for example:

entry

id=1 requirements=(1,2,3)

id=2 requirements=(1,3,5)

submission

id=1 entry=1 requirement=1

id=2 entry=2 requirement=2

id=3 entry=3 requirement=3

Would select entry id=1, but not entry id=2 (so WHERE IN doesn't do the job)

*edit actual datasets:

INSERT INTO submission (entry_id, requirement_id, submission_id) VALUES
(17, 1, 1),
(43, 1, 2),
(57, 0, 3),
(57, 0, 4),
(1, 1, 5),
(26, 1, 6),
(40, 1, 7),
(40, 1, 8),
(40, 1, 9),
(40, 1, 10),
(85, 1, 11),
(94, 1, 12),
(114, 0, 13),
(32, 1, 14),
(34, 0, 15);

INSERT INTO entry (entry_id, category_id) VALUES
(1, 2),
(2, 1),
(3, 1),
(4, 1),
(5, 2),
(6, 1),
(7, 1),
(8, 1),
(9, 1),
(10, 1),
(11, 1),
(12, 1),
(13, 1),
(14, 1),
(15, 1);

INSERT INTO category (category_id, requirement) VALUES
(1, '1,2,3'),
(2, '1,2,4,5,6'),
(3, '1,2,4,5,6');

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

以酷 2024-10-23 16:53:13

查找表 1 中的所有行,其中表 2 中有 3 个匹配行:

SELECT
  table1.id
FROM
  table1
INNER JOIN
  table2
ON
  table1.somecol = table2.somecol
GROUP BY
  table1.id
HAVING
  COUNT(*) = 3

我会为您的数据编写实际的查询,但我无法解读您的问题。

Find all rows in table 1 where there are 3 matching rows in table 2:

SELECT
  table1.id
FROM
  table1
INNER JOIN
  table2
ON
  table1.somecol = table2.somecol
GROUP BY
  table1.id
HAVING
  COUNT(*) = 3

I'd write the actual query for your data but I can't decipher your question.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文