my $sql_count_A_Ref_inters_Comp = " DROP TABLE IF EXISTS e_annotation_090812.TEMP_reference_table; CREATE TABLE e_annotation_090812.TEMP_reference_table ( Key_m INT NOT NULL AUTO_INCREMENT, Entity_name_copy varchar(254) NOT NULL, PRIMARY KEY (Key_m) ); INSERT INTO e_annotation_090812.TEMP_reference_table (Entity_name_copy) SELECT A.entity_name FROM e_annotation_090812.annotation A join e_annotation_090812.Temp_table T on T.Line=A.user where A.centre like '".$Reference."' and A.entity_name like '_%' and A.evidence_code not like '%_________8__'; SELECT '".$Reference."', '".$Comparitor."', A.entity_name, A.* FROM e_annotation_090812.annotation A join e_annotation_090812.Temp_table T on T.Line=A.user where A.entity_name like '_%' and A.evidence_code not like '%_________8__' and A.centre like '".$Comparitor."' and A.entity_name IN ( SELECT E.Entity_name_copy FROM e_annotation_090812.TEMP_reference_table E ); ";