create database if not exists test;

use test;

create table table_a (
    a_id int auto_increment primary key ,
    uuid varchar(38) default uuid(),
    name varchar(200) not null
);

create table table_b (
    uuid varchar(38) default uuid() primary key
);

create table table_c (
    c_id int primary key auto_increment,
    a_id int not null,
    uuid varchar(38) not null,
    constraint fk_table_c_table_a foreign key (a_id) references table_a(a_id)
);

create table table_d
(
    d_id int auto_increment primary key,
    a_id int null,
    name varchar(100) null,
    uuid char(36) null,
    c_id int null
);

insert into `table_d` (`name`, `uuid`, `c_id`)
select distinct (
    case
        when (x2.x4 is null) then null
        else (substring(x2.x4, 1, 100))
    end),
   case
       when (x2.x16 is not null) then x2.x17
       else null
   end,
   x2.x18
from (select x19.x23 as x18, x19.x32 as x33, x19.x81 as x4, x19.x90 as x16, x19.x139 as x17, x19.x140 as x141,
          (case when (x216.`d_id` is null) then null else 1 end) as x217
          from (
              select x255.x258 as x23, x255.x264 as x32, x255.x267 as x37,
                x255.x293 as x81,
                x255.x298 as x90,
                x255.x323 as x139,
                (case when (x324.`d_id` is null) then null else 1 end) as x140
            from (
                select
                       x325.`c_id` as x258,
                       x325.`a_id` as x264,
                       x325.`uuid` as x267,
                      x326.`name` as x293,
                      (case when (x327.`uuid` is null) then null else 1 end) as x298,
                      x327.`uuid` as x323
                  from `table_c` x325
                           inner join `table_a` x326 on x325.`a_id` = x326.`a_id`
                           left outer join `table_b` x327 on x327.`uuid` = x326.`uuid`) x255
                     left outer join `table_d` x324 on x324.`c_id` = x255.x258) x19
               left outer join `table_d` x216 on (x19.x37 = x216.`uuid`) and false) x2
         left outer join `table_d` x328 on (x2.x33 = x328.`a_id`)
where (((x2.x141 is null) and (x2.x217 is null)) and (x328.`d_id` is null));
