Get the count of other users having the x tag with order_id
in order_id
of users having the y tag
table: MyCustomTable
(200K rows)
order_id
user_id
type
('x'
and'y'
) my custom tags
and the query:
SELECT COUNT(order_id),user_id
FROM MyCustomTable
WHERE type='x'
AND order_id IN (SELECT order_id
FROM MyCustomTable
WHERE type ='y'
AND user_id='56')
AND user_id <> '56'
GROUP BY user_id
Create table output
CREATE TABLE `MyCustomTable` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`order_id` int(11) NOT NULL,
`user_id` int(11) NOT NULL,
`type` enum('x','y') NOT NULL DEFAULT 'x',
PRIMARY KEY (`id`),
KEY `customeKey` (`user_id`,`type`,`order_id`),
) ENGINE=InnoDB AUTO_INCREMENT=360806 DEFAULT CHARSET=utf8
SHOW CREATE TABLE MyCustomTable;