select distinct(ext_prefix) from trunks_rules where CHAR_LENGTH(ext_prefix) = 7 and ext_prefix like '6%'; call this A
select right(sim_bank.scid_gsm.gsm_number,7) from asterisk.trunks_trunk,sim_bank.scid_gsm where asterisk.trunks_trunk.scid_date > '1 hour ago' and [login to view URL] = [login to view URL] ; Call this B
If an entry in A does not exist in B , delete it from A
if an entry in B does not exist in A
select count(trunk_id), [login to view URL] from asterisk.trunks_rules,asterisk.trunks_trunk where trunks_trunk.id=trunks_rules.trunk_id and trunks_trunk.trunk_type='Dongle' group by trunk_id;
find the 7 smallest counts eg (55,24,65,78,62,45,23)
for every entry found in B that does not exist in A
insert into trunk_rules (trunk_id,ext_prefix,strip,allow) values ('55','B[1]','0','1')
insert into trunk_rules (trunk_id,ext_prefix,strip,allow) values ('24','B[1]','0','1')
insert into trunk_rules (trunk_id,ext_prefix,strip,allow) values ('65','B[1]','0','1')
insert into trunk_rules (trunk_id,ext_prefix,strip,allow) values ('78','B[1]','0','1')
insert into trunk_rules (trunk_id,ext_prefix,strip,allow) values ('62','B[1]','0','1')
insert into trunk_rules (trunk_id,ext_prefix,strip,allow) values ('45','B[1]','0','1')
insert into trunk_rules (trunk_id,ext_prefix,strip,allow) values ('23','B[1]','0','1')
find 7 smallest count again and insert B[2]
find 7 smallest count again and insert B[3]
etc
DB structure in attachment