By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
CREATE TABLE orders (
id INT,
userId INT
)
INSERT INTO `orders` (`id`, `userId`) VALUES (1, 10), (2, 10), (3, 10)
SELECT `id`, `userId`
FROM `orders` AS `bonus_program`
id | userId |
---|---|
1 | 10 |
2 | 10 |
3 | 10 |
SELECT `userId`, COUNT(*) AS `order_counts` FROM `orders` GROUP BY `userId` HAVING `order_counts` >= 3
userId | order_counts |
---|---|
10 | 3 |
CREATE TABLE bonuses (
id INT,
userId INT
)
INSERT INTO `bonuses` (`id`, `userId`) VALUES (1, 10), (2, 10), (3, 11), (3, 10)
SELECT `id`, `userId`
FROM `bonuses` AS `bonuses`
id | userId |
---|---|
1 | 10 |
2 | 10 |
3 | 11 |
3 | 10 |
SELECT `userId`, COUNT(*) AS `bonuses_counts` FROM `bonuses` GROUP BY `userId`
userId | bonuses_counts |
---|---|
10 | 3 |
11 | 1 |