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

时间:2011-02-09 06:42:00

标签: mysql select

我有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 submissionentry_idrequirement_idsubmission_id)价值观 (17,1,1), (43,4,2), (57,0,3), (57,5,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 entryentry_idcategory_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 categorycategory_idrequirement)VALUES (1,'1,2,3'), (2,'1,2,4,5,6'), (3,'1,2,4,5,6');

1 个答案:

答案 0 :(得分:0)

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

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

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