Tentu, Anda dapat melakukannya dalam satu kueri. Menggunakan tabel contoh ini:
create table duotri (time varchar(100), type int, genre int, doubles int, triples int);
insert duotri values
('2010.06.21 12:00' ,1 ,1 ,0 ,0),
('2010.06.21 12:00' ,1 ,2 ,0 ,0),
('2010.06.21 12:00' ,1 ,1 ,0 ,0),
('2010.06.21 12:00' ,2 ,3 ,0 ,0),
('2010.06.22 12:00' ,2 ,2 ,0 ,0),
('2010.06.22 12:00' ,2 ,3 ,0 ,0),
('2010.06.22 12:00' ,1 ,1 ,0 ,0);
Pernyataan pembaruan harus bergabung ke formulir GROUPed untuk mendapatkan ganda dan tiga kali lipat.
update duotri t1
inner join (
select time, type,
case when count(distinct genre) = 2 then 1 else 0 end doubles,
case when count(distinct genre) = 3 then 1 else 0 end triples
from duotri
group by time, type) t2
on t1.time=t2.time and t1.type=t2.type
set t1.doubles=t2.doubles, t1.triples = t2.triples;