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(9180,9181,9182,9183,9196,9197,9207,9209,9213,9214,9233,9256,9263,9274,9276,9278,9298,9315,9316,9317,9319,9342,9373,9411,9425,9428,9433,9457,9469,9480,9482,9500,9505,9507,9520,9537,9594,25908,47534,54655,55827,58516,58517,58518,58825,59246,66368,66371,66372,66459,66480,67136,67154,69248,79262,79263,79664,93073,95847,96234,98299,99896,109057,113078) and (pr.validfrom is null or (validfrom>='2021-04-23')) and (pr.expires is null or (expires>='2021-04-23')) and pr.hotelduration=10 and pr.tourduration=10 and pr.departuredate='2020-07-22' 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='267' ;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 ;