#---------------- table1 -------------------
CREATE TABLE table1 (
  `ID` int(10) unsigned NOT NULL AUTO_INCREMENT,
  HEAD_ID int(11) NOT NULL,
  `CODE` varchar(2) NOT NULL,
  `DATA` varchar(255) NOT NULL,
  PRIMARY KEY (`ID`),
  KEY head_id_index (HEAD_ID)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_hungarian_ci;

INSERT INTO table1 VALUES
(1,1,'BB','dfsdfas'),
(2,1,'BB','142ase'),
(3,2,'AA','sfdgfdg');

#---------------- table2 -------------------
CREATE TABLE table2 (
  `ID` int(10) unsigned NOT NULL AUTO_INCREMENT,
  FK_ID int(11) NOT NULL,
  FK_HEAD_ID int(11) NOT NULL,
  `CODE` varchar(2) NOT NULL,
  column1 varchar(255) NOT NULL,
  PRIMARY KEY (`ID`),
  KEY fk_id_index (FK_ID),
  KEY fk_head_id_index (FK_HEAD_ID),
  KEY code_index (`CODE`)
) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_hungarian_ci;

INSERT INTO table2 VALUES
(1,1,1,'BB','33'),
(2,2,1,'BB','gg'),
(3,3,2,'AA','fvfd'),
(4,3,8,'CC','ewr'),
(5,4,4,'GG','er'),
(6,5,7,'TT','sdf'),
(7,3,6,'BB',''),
(8,2,3,'BB',''),
(9,0,0,'BB',''),
(10,2,9,'BB',''),
(13,1,4,'BB',''),
(14,0,6,'BB','');

DELETE FROM table2 USING table1 
LEFT OUTER JOIN table2 ON table2.fk_id=table1.id 
AND table2.code=table1.code  
AND table2.fk_head_id=table1.head_id 
WHERE table1.head_id=1;
