drop temporary table if exists TP; create temporary table TP ( #PRIMARY KEY (id), #INDEX temp__prid (id), INDEX temp__marketid (wpid), INDEX temp__price (bed,ext,chd,bedpricesfinal), INDEX temp__package (packageid,packagename) ) select pr.*,P.post_title ,idx.countryid,cnt.name countryname ,idx.cityid,cty.name cityname ,idx.regionid,rgn.name regionname ,idx.servicetypeid,sta.name servicetypename ,idx.generaltypeid,gta.name generaltypename from ( select pr.* from wpone_tourpricev2final pr where pr.wpid in(9179,9195,9223,9242,9248,9261,9275,9277,9280,9283,9284,9285,9307,9324,9325,9326,9361,9375,9376,9381,9407,9408,9417,9422,9427,9431,9499,9502,9508,9527,9541,9544,9553,9554,9564,9585,9598,9611,16543,44888,54304,56908,58199,59192,67134,67135,67139,67140,67141,67144,67156,67157,67158,83125,95927) and (pr.validfrom is null or (validfrom>='2020-10-26')) and (pr.expires is null or (expires>='2020-10-26')) and pr.hotelduration=10 and pr.tourduration=10 and pr.departuredate='2020-07-19' and pr.bed+pr.ext=2 and pr.inf>=0 and pr.chd=0 ) pr left join 0Wjxn7YMR_posts P on pr.wpid=P.ID left join wpone_hotelindex idx on idx.ID=pr.wpid left join 0Wjxn7YMR_terms cnt on cnt.term_id=idx.countryid left join 0Wjxn7YMR_terms cty on cty.term_id=idx.cityid left join 0Wjxn7YMR_terms rgn on rgn.term_id=idx.regionid left join 0Wjxn7YMR_terms sta on sta.term_id=idx.servicetypeid left join 0Wjxn7YMR_terms gta on gta.term_id=idx.generaltypeid where P.post_status='publish' and idx.countryid='260' and idx.cityid='266' ;drop temporary table if exists TPORDER; create temporary table TPORDER select packageid temppackageid, wpid temppostid, min(bedpricesfinal) minprice from TP where bed=2 and ext=0 and chd=0 group by packageid,wpid;select tp.* from TP tp left join TPORDER tpo on tpo.temppostid=tp.wpid and tpo.temppackageid=tp.packageid order by tpo.minprice,tp.wpid,tp.bed,tp.ext,tp.bedpricesfinal,tp.ext,tp.chd,tp.min1,tp.min2,tp.min3,tp.min4 ;