With the following statements we can reproduce the issue: CREATE TABLE `company_element` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `type` varchar(30) NOT NULL COMMENT 'location_unit, organisational_unit, location_unit_root, organisational_unit_root', `company_element_id` int(10) unsigned DEFAULT NULL COMMENT 'parent column', `lft` int(10) unsigned DEFAULT NULL, `rgt` int(10) unsigned DEFAULT NULL, `level` int(10) unsigned DEFAULT NULL, `root_id` int(10) unsigned DEFAULT NULL, `is_active` tinyint(1) unsigned DEFAULT 1, `active_start_date` date DEFAULT NULL, `active_end_date` date DEFAULT NULL, `visibility_group_guid` varchar(36) DEFAULT NULL, `name` varchar(255) DEFAULT NULL, `root_name` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`), KEY `root_id` (`root_id`), KEY `level` (`level`), KEY `rgt` (`rgt`), KEY `lft` (`lft`), KEY `IX_tree` (`root_id`,`level`,`lft`,`rgt`), KEY `IX_type` (`type`), KEY `company_element_id` (`company_element_id`), KEY `FK_visibility_group_guid` (`visibility_group_guid`), KEY `IX_virtual_name` (`name`), KEY `IX_virtual_root_name` (`root_name`), CONSTRAINT `company_element_ibfk_1` FOREIGN KEY (`company_element_id`) REFERENCES `company_element` (`id`) ON DELETE CASCADE ON UPDATE CASCADE ) ENGINE=InnoDB AUTO_INCREMENT=6920 DEFAULT CHARSET=utf8; -- some foreign_keys not created because referencing tables don't exist -- only a test case CREATE TABLE `haz_mat` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `resource_id` int(10) unsigned NOT NULL, `substitute_haz_mat_id` int(10) unsigned DEFAULT NULL, `is_active` tinyint(1) unsigned NOT NULL DEFAULT 1, `active_start_date` date DEFAULT NULL, `active_end_date` date DEFAULT NULL, `discouraged_usage` varchar(2000) DEFAULT NULL, `shape` varchar(200) DEFAULT NULL, `color` varchar(200) DEFAULT NULL, `odor` varchar(200) DEFAULT NULL, `hydro_carbon_mixture` varchar(15) DEFAULT NULL COMMENT 'Kohlenwasserstoffgemische', `ignitability` varchar(45) DEFAULT NULL COMMENT 'Entzündbarkeit', `flash_point` varchar(20) DEFAULT NULL COMMENT 'Flammpunkt', `flash_point_measurement_type` varchar(120) DEFAULT NULL, `ignition_temperature` varchar(20) DEFAULT NULL COMMENT 'Zündtemperatur', `temperature_class` varchar(120) DEFAULT NULL, `combustion_factor` tinyint(1) unsigned DEFAULT NULL, `boiling_temperature` decimal(6,2) DEFAULT NULL, `explosion_group` varchar(45) DEFAULT NULL COMMENT 'Explosionsgruppe', `dust_explosion_class` varchar(120) DEFAULT NULL, `density_ratio_air` varchar(45) DEFAULT NULL COMMENT 'Dichteverhältnis zu Luft', `density_ratio_air_at_temperature` decimal(6,2) DEFAULT NULL, `vapor_pressure` decimal(20,3) unsigned DEFAULT NULL, `vapor_pressure_at_temperature` decimal(6,2) DEFAULT NULL, `lower_explosion_limit` varchar(20) DEFAULT NULL COMMENT 'untere Explosionsgrenze', `upper_explosion_limit` varchar(20) DEFAULT NULL COMMENT 'obere Explosionsgrenze', `ph_value` decimal(3,1) unsigned DEFAULT NULL, `ph_value_to` decimal(3,1) unsigned DEFAULT NULL, `signal_word` varchar(45) DEFAULT NULL, `cas_number` varchar(200) DEFAULT NULL, `eu_number` varchar(200) DEFAULT NULL, `echa_number` varchar(200) DEFAULT NULL, `article_number` varchar(45) DEFAULT NULL, `waste_number` varchar(100) DEFAULT NULL, `solvent_content_percent` decimal(5,2) unsigned DEFAULT NULL, `solvent_content_volume` decimal(7,3) unsigned DEFAULT NULL, `is_working_material` tinyint(1) unsigned DEFAULT NULL, `water_hazard_category` tinyint(2) unsigned DEFAULT NULL, `is_hazardous_incident_ordinance_enabled` tinyint(1) unsigned NOT NULL DEFAULT 0, `hazardous_incident_ordinance` varchar(5) DEFAULT NULL, `hazardous_incident_ordinance_seveso_backup` varchar(5) DEFAULT NULL, `is_ghs_backup` tinyint(1) unsigned NOT NULL DEFAULT 1, `is_gefstoffv` tinyint(1) unsigned NOT NULL DEFAULT 0, `oelv` varchar(2000) DEFAULT NULL COMMENT 'occupational exposure limit \r\n\r\nvalue', `blv` varchar(2000) DEFAULT NULL COMMENT 'biological limit value', `other_hazards` varchar(2000) DEFAULT NULL, `storage_class` varchar(5) DEFAULT NULL, `production_origin` varchar(10) DEFAULT NULL, `danger_case_phone_number` varchar(20) DEFAULT NULL, `fire_protection_description` varchar(2000) DEFAULT NULL, `is_disposal` tinyint(1) unsigned NOT NULL DEFAULT 0, `classification` tinyint(1) unsigned NOT NULL DEFAULT 3 COMMENT '3=open,2=no_hazard_good,1=hazard_good', `disposal_phone_number` varchar(20) DEFAULT NULL, `disposal_description` varchar(2000) DEFAULT NULL, `safety_measure_phone_number` varchar(20) DEFAULT NULL, `safety_measure_description` varchar(2000) DEFAULT NULL, `first_aid_phone_number` varchar(20) DEFAULT NULL, `first_aid_description` varchar(2000) DEFAULT NULL, `operating_instr_document_node_id` int(10) unsigned DEFAULT NULL, `report_layout` int(1) unsigned NOT NULL DEFAULT 1 COMMENT '1=columns, 2=rows', `report_settings_id` int(10) unsigned DEFAULT NULL, `report_language` varchar(10) DEFAULT NULL, `sds_document_node_id` int(10) unsigned DEFAULT NULL, `is_sds_check_disabled` tinyint(1) unsigned NOT NULL DEFAULT 0, `sds_status` tinyint(1) unsigned NOT NULL DEFAULT 0, `date_sds` date DEFAULT NULL, `date_sds_print` date DEFAULT NULL, `sds_requested_on` date DEFAULT NULL, `is_aggregation_material` tinyint(1) unsigned DEFAULT 0, `visibility_group_guid` varchar(36) DEFAULT NULL, `created_on` datetime DEFAULT NULL, `created_by_id` int(10) unsigned DEFAULT NULL, `created_by` varchar(263) DEFAULT NULL, `saved_on` datetime DEFAULT NULL, `saved_by_id` int(10) unsigned DEFAULT NULL, `saved_by` varchar(263) DEFAULT NULL, `locale_code` varchar(5) DEFAULT NULL, PRIMARY KEY (`id`), UNIQUE KEY `FK_resource_id` (`resource_id`), KEY `FK_operating_instr_document_node_id` (`operating_instr_document_node_id`), KEY `sds_status` (`sds_status`), KEY `FK_report_settings_id` (`report_settings_id`), KEY `IX_is_working_material` (`is_working_material`), KEY `IX_date_sds_print` (`date_sds_print`), KEY `IX_date_sds` (`date_sds`), KEY `IX_sds_requested_on` (`sds_requested_on`), KEY `FK_substitute_haz_mat_id` (`substitute_haz_mat_id`), KEY `IX_is_active_state` (`is_active`,`active_end_date`), KEY `IX_classification` (`classification`), KEY `FK_sds_document_node_id` (`sds_document_node_id`), KEY `IX_locale_code` (`locale_code`), KEY `FK_visibility_group_guid` (`visibility_group_guid`), CONSTRAINT `FK_substitute_haz_mat_id` FOREIGN KEY (`substitute_haz_mat_id`) REFERENCES `haz_mat` (`id`) ON DELETE SET NULL ON UPDATE CASCADE ) ENGINE=InnoDB AUTO_INCREMENT=1072 DEFAULT CHARSET=utf8; -- some foreign_keys not created because referencing tables don't exist -- only a test case CREATE TABLE `haz_mat_approval_statement` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `haz_mat_id` int(10) unsigned NOT NULL, `scope_id_backup` int(10) unsigned DEFAULT NULL, `state` varchar(25) NOT NULL DEFAULT 'in_process' COMMENT 'in_process | approved | with_reservation | not_allowed', `is_whole_company` tinyint(1) unsigned NOT NULL DEFAULT 0, `usage` varchar(2000) NOT NULL, `date_start` date NOT NULL, `date_end` date NOT NULL, `description` varchar(2000) DEFAULT NULL, `document_node_id` int(10) unsigned DEFAULT NULL, `report_settings_id` int(10) unsigned DEFAULT NULL, `is_display_empty_fields_in_report` tinyint(1) unsigned NOT NULL DEFAULT 0, `report_language` varchar(10) DEFAULT NULL, `document_revision_id` int(10) unsigned DEFAULT NULL, `person_id` int(10) unsigned DEFAULT NULL, `is_closed` tinyint(1) unsigned NOT NULL DEFAULT 0, `mail_sent` int(1) unsigned NOT NULL DEFAULT 0, `date_mail_sent` datetime DEFAULT NULL, `visibility_group_guid` varchar(36) DEFAULT NULL, `locale_code` varchar(5) DEFAULT NULL, `created_on` datetime DEFAULT NULL, `created_by_id` int(10) unsigned DEFAULT NULL, `created_by` varchar(263) DEFAULT NULL, `saved_on` datetime DEFAULT NULL, `saved_by_id` int(10) unsigned DEFAULT NULL, `saved_by` varchar(263) DEFAULT NULL, `escalation_object_id` int(10) unsigned DEFAULT NULL, PRIMARY KEY (`id`), KEY `FK_haz_mat_id` (`haz_mat_id`), KEY `FK_document_node_id` (`document_node_id`), KEY `FK_visibility_group_guid` (`visibility_group_guid`), KEY `FK_report_settings_id` (`report_settings_id`), KEY `IX_date_end` (`date_end`), KEY `IX_is_closed` (`is_closed`), KEY `FK_document_revision_id` (`document_revision_id`), KEY `FK_person_id` (`person_id`), KEY `FK_haz_mat_approval_statement_process_escalation_object` (`escalation_object_id`), KEY `IX_locale_code` (`locale_code`), CONSTRAINT `haz_mat_approval_statement_ibfk_1` FOREIGN KEY (`haz_mat_id`) REFERENCES `haz_mat` (`id`) ON DELETE CASCADE ON UPDATE CASCADE ) ENGINE=InnoDB AUTO_INCREMENT=78 DEFAULT CHARSET=utf8; -- CREATE TABLE `haz_mat_approval_statement2company_element` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `haz_mat_approval_statement_id` int(10) unsigned NOT NULL, `company_element_id` int(10) unsigned NOT NULL, `created_on` datetime DEFAULT NULL, `created_by_id` int(10) unsigned DEFAULT NULL, `created_by` varchar(263) DEFAULT NULL, PRIMARY KEY (`id`), UNIQUE KEY `UNIQUE` (`haz_mat_approval_statement_id`,`company_element_id`), KEY `FK_haz_mat_approval_statement_id` (`haz_mat_approval_statement_id`), KEY `FK_company_element_id` (`company_element_id`), CONSTRAINT `haz_mat_approval_statement2company_element_ibfk_1` FOREIGN KEY (`haz_mat_approval_statement_id`) REFERENCES `haz_mat_approval_statement` (`id`) ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT `haz_mat_approval_statement2company_element_ibfk_2` FOREIGN KEY (`company_element_id`) REFERENCES `company_element` (`id`) ON DELETE CASCADE ON UPDATE CASCADE ) ENGINE=InnoDB AUTO_INCREMENT=29 DEFAULT CHARSET=utf8; -- Create the views: CREATE OR REPLACE ALGORITHM=UNDEFINED SQL SECURITY DEFINER VIEW `haz_mat_approval_statement_approval_state_nested_view` AS select `hmas`.`id` AS `haz_mat_approval_statement_id`,`hmas`.`haz_mat_id` AS `haz_mat_id`,`hmas`.`date_end` AS `date_end`,`hmas`.`state` AS `state`,`hmas`.`is_whole_company` AS `is_whole_company`,`ce`.`id` AS `company_element_id`,`ce`.`lft` AS `lft`,`ce`.`rgt` AS `rgt` from ((`haz_mat_approval_statement` `hmas` left join `haz_mat_approval_statement2company_element` `hmas2ce` on(`hmas`.`is_whole_company` = 0 and `hmas2ce`.`haz_mat_approval_statement_id` = `hmas`.`id`)) left join `company_element` `ce` on(`ce`.`id` = `hmas2ce`.`company_element_id`)); CREATE OR REPLACE ALGORITHM=UNDEFINED SQL SECURITY DEFINER VIEW `haz_mat_approval_statement_approval_state_view` AS select `hm`.`id` AS `haz_mat_id`,ifnull(`v1`.`haz_mat_approval_statement_id`,`v3`.`haz_mat_approval_statement_id`) AS `haz_mat_approval_statement_id`,ifnull(`v1`.`is_whole_company`,`v3`.`is_whole_company`) AS `is_whole_company`,`v1`.`company_element_id` AS `company_element_id`,`v1`.`lft` AS `lft`,`v1`.`rgt` AS `rgt`,ifnull(`v1`.`state`,`v3`.`state`) AS `state`,ifnull(`v1`.`date_end`,`v3`.`date_end`) AS `date_end` from ((((`haz_mat` `hm` left join `haz_mat_approval_statement_approval_state_nested_view` `v1` on(`v1`.`haz_mat_id` = `hm`.`id` and `v1`.`is_whole_company` = 0)) left join `haz_mat_approval_statement_approval_state_nested_view` `v2` on(`v2`.`haz_mat_id` = `hm`.`id` and `v2`.`is_whole_company` = 0 and `v2`.`company_element_id` = `v1`.`company_element_id` and `v2`.`date_end` > `v1`.`date_end`)) left join `haz_mat_approval_statement_approval_state_nested_view` `v3` on(`v3`.`haz_mat_id` = `hm`.`id` and `v3`.`is_whole_company` = 1 and `v1`.`haz_mat_id` is null)) left join `haz_mat_approval_statement_approval_state_nested_view` `v4` on(`v4`.`haz_mat_id` = `hm`.`id` and `v4`.`is_whole_company` = 1 and `v1`.`haz_mat_id` is null and `v4`.`date_end` > `v3`.`date_end`)) where `v2`.`haz_mat_id` is null and `v4`.`haz_mat_id` is null and (`v1`.`haz_mat_id` is not null or `v3`.`haz_mat_id` is not null); -- Statement ok SELECT h9.id FROM haz_mat h9 LEFT JOIN haz_mat_approval_statement_approval_state_view h14 ON h9.id = h14.haz_mat_id LEFT JOIN haz_mat_approval_statement_approval_state_view h15 ON h9.id = h15.haz_mat_id; -- Failing Statment SELECT DISTINCT h9.id FROM haz_mat h9 LEFT JOIN haz_mat_approval_statement_approval_state_view h14 ON h9.id = h14.haz_mat_id LEFT JOIN haz_mat_approval_statement_approval_state_view h15 ON h9.id = h15.haz_mat_id; --> ERROR 2013 (HY000): Lost connection to MySQL server during query