$result = mysql_query("SELECT id FROM db_topics WHERE mainnumber='$mainid'"); $ids = Array(); while($row = mysql_fetch_array($result)){ $ids[] = $row['id']; } $sql = "DELETE FROM t1.*, t2.* FROM db_thread t1, db_thread t2 WHERE " . "t1.threadid IN (" . implode(', ', $ids) . ") AND " . "t1.threadid=t2.id"; mysql_query($sql); /* while ($row = mysql_fetch_array($result)) { $topicid = $row['id']; mysql_query("DELETE from db_thread WHERE threadid='$topicid'") or die(mysql_error()); mysql_query("DELETE from db_topics WHERE id='$topicid'") or die(mysql_error()); } */