В MySQL, если у вас есть следующие списки:
list1 = ('1', '3')
list2 = ('2', '3')
list3 = ('3', '5')
тогда вы можете сделать это:
SELECT
TransactionID,
SUM(ItemID IN ('1', '3')) AS list1_count,
SUM(ItemID IN ('2', '3')) AS list2_count,
SUM(ItemID IN ('3', '5')) AS list3_count
FROM table1
GROUP BY TransactionID
HAVING list1_count > 0 AND list2_count > 0 AND list3_count > 0
Результат:
TransactionId list1_count list2_count list3_count
1 3 2 1
3 2 2 1
Данные испытаний:
CREATE TABLE table1 (ID INT NOT NULL, TransactionID INT NOT NULL, ItemID INT NOT NULL);
INSERT INTO table1 (ID, TransactionID, ItemID) VALUES
(1, 1, 1),
(2, 1, 2),
(3, 1, 3),
(4, 1, 4),
(5, 1, 1),
(6, 2, 1),
(7, 2, 2),
(8, 2, 1),
(9, 2, 4),
(10, 3, 3),
(11, 3, 2),
(12, 3, 1);