add batch remove batch split batch comment selection show hidden batches hide batch highlight batch
db<>fiddle
donate feedback about
By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
CREATE TABLE table1 (
`id` INTEGER,
`category` VARCHAR(1)
);

INSERT INTO table1
(`id`, `category`)
VALUES
('0', 'A'),
('1', 'A'),
('2', 'A'),
('3', 'A'),
('4', 'A'),
('5', 'A'),
('6', 'A'),
('7', 'A'),
('8', 'A'),
('9', 'A'),
('10', 'A'),
('11', 'A'),
('12', 'A'),
('13', 'A'),
('14', 'A'),
('15', 'A'),
('16', 'A'),
('17', 'A'),
('18', 'A'),
('19', 'A'),
('20', 'A'),
('21', 'A'),
('22', 'A'),
('23', 'A'),
('24', 'A'),
('25', 'A'),
('26', 'A'),
('27', 'A'),
id category
0 A
1 A
2 A
3 A
4 A
5 A
6 A
7 A
8 A
9 A
10 A
11 A
12 A
13 A
14 A
15 A
16 A
17 A
18 A
19 A
20 A
21 A
22 A
23 A
24 A
25 A
26 A
27 A
28 A
29 A
30 B
31 B
32 B
33 B
34 B
35 B
36 B
37 B
38 B
39 B
40 B
41 B
42 B
43 B
44 B
45 B
46 B
47 B
48 B
49 B
50 B
51 B
52 B
53 B
54 B
55 B
56 B
57 B
58 B
59 B
SELECT * FROM table1 t1
WHERE NOT EXISTS (
SELECT 1 FROM (
(SELECT * FROM table1 WHERE category = 'A' ORDER BY RAND() LIMIT 4)
UNION ALL
(SELECT * FROM table1 WHERE category = 'B' ORDER BY RAND() LIMIT 4)
) t
WHERE t.id = t1.id AND t.category = t1.category
)
id category
1 A
2 A
3 A
4 A
5 A
6 A
7 A
8 A
9 A
10 A
11 A
13 A
14 A
15 A
16 A
17 A
18 A
19 A
20 A
21 A
22 A
23 A
24 A
25 A
26 A
28 A
30 B
31 B
32 B
33 B
34 B
35 B
36 B
38 B
39 B
41 B
43 B
44 B
45 B
46 B
47 B
48 B
49 B
50 B
51 B
52 B
53 B
54 B
56 B
57 B
58 B
59 B