Matching at least one of each value in MySQL
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');
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.
精彩评论