 SET GLOBAL log_output = 'TABLE';
 SET GLOBAL general_log = 'ON';
\W
use test;
drop table t2,t1;

CREATE or replace TABLE `t1` (
  `id` int(10) unsigned NOT NULL ,
  `onsite_consult_id` int(10) unsigned NOT NULL,
  `deleted_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `f1` (`onsite_consult_id`)
) ENGINE=Innodb ;
INSERT INTO `t1` VALUES (1,58,NULL),(2,58,NULL),(3,56,NULL),(4,58,NULL),(5,56,NULL),(6,58,NULL),(7,57,NULL),(8,55,NULL),(9,58,NULL),(10,56,NULL);

CREATE or replace TABLE `t2` (
  `id` int(10) unsigned NOT NULL ,
  `focus_id` int(10) unsigned NOT NULL,
  `deleted_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `f2` (`focus_id`)
 ) ENGINE=Innodb ;
INSERT INTO `t2` VALUES (1,1,NULL),(2,2,NULL),(3,3,NULL),(4,4,NULL),(5,5,NULL),(6,6,NULL),(7,7,NULL),(8,8,NULL),(9,9,NULL),(10,10,NULL);


prepare stmt1 from "
SELECT count(*) FROM `test`.`t1`
WHERE `onsite_consult_id` = '58'
  AND EXISTS
    (SELECT *
     FROM `test`.`t2`
     WHERE `test`.`t1`.`id` = `test`.`t2`.`focus_id`
       AND `id` != '12'
       AND `test`.`t2`.`deleted_at` IS NULL)
  AND EXISTS
    (SELECT *
     FROM `test`.`t2`
     WHERE `test`.`t1`.`id` = `test`.`t2`.`focus_id`
       AND `id` != '7'
       AND `test`.`t2`.`deleted_at` IS NULL)
  AND EXISTS
    (SELECT *
     FROM `test`.`t2`
     WHERE `test`.`t1`.`id` = `test`.`t2`.`focus_id`
       AND `id` != '9'
       AND `test`.`t2`.`deleted_at` IS NULL)
  AND EXISTS
    (SELECT *
     FROM `test`.`t2`
     WHERE `test`.`t1`.`id` = `test`.`t2`.`focus_id`
       AND `id` != '11'
       AND `test`.`t2`.`deleted_at` IS NULL)
  AND EXISTS
    (SELECT *
     FROM `test`.`t2`
     WHERE `test`.`t1`.`id` = `test`.`t2`.`focus_id`
       AND `id` != '4'
       AND `test`.`t2`.`deleted_at` IS NULL)
  AND EXISTS
    (SELECT *
     FROM `test`.`t2`
     WHERE `test`.`t1`.`id` = `test`.`t2`.`focus_id`
       AND `id` != '10'
       AND `test`.`t2`.`deleted_at` IS NULL)
  AND EXISTS
    (SELECT *
     FROM `test`.`t2`
     WHERE `test`.`t1`.`id` = `test`.`t2`.`focus_id`
       AND `id` != '1'
       AND `test`.`t2`.`deleted_at` IS NULL)
  AND EXISTS
    (SELECT *
     FROM `test`.`t2`
     WHERE `test`.`t1`.`id` = `test`.`t2`.`focus_id`
       AND `id` != '2'
       AND `test`.`t2`.`deleted_at` IS NULL)
  AND EXISTS
    (SELECT *
     FROM `test`.`t2`
     WHERE `test`.`t1`.`id` = `test`.`t2`.`focus_id`
       AND `id` != '3'
       AND `test`.`t2`.`deleted_at` IS NULL)
  AND `test`.`t1`.`deleted_at` IS NULL";

execute stmt1;
select 1;