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(9186,9212,9215,9216,9217,9218,9224,9226,9245,9290,9291,9292,9344,9345,9384,9385,9386,9387,9388,9389,9413,9414,9415,9430,9435,9436,9437,9441,9447,9448,9465,9470,9525,9600,9601,9605,41818,54667,58842,58915,59032,59076,59140,59324,66367,66370,66373,66402,66403,66404,66405,66406,67910,67911,67912,67913,68837,69759,71276,71344,71431,80401,80602,92542,92628,92953) 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='275' ;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 ;