select d.*, IF('bg'='bg' or dt.title is null,d.title,dt.title) as title, IF('bg'='bg' or dt.title is null,d.link,dt.link) as link, IF('bg'='bg' or dt.title is null,d.short_title,dt.short_title) as short_title, IF(d.is_booking = 1,MIN(db.price),dv.price_normal) as price_normal, dv.price_discount, sum(dv.fake_count) + sum(dv.now_count) as fake_count from deals as d left join partners as p on p.id = d.partner_id left join deals_titles as dt on dt.deal_id = d.id and dt.lang = 'bg' left join deals_variants as dv on dv.deal_id=d.id and dv.deleted=0 left join deals_categories as dcc on dcc.deal_id = d.id left join deals_periods as db on db.deal_id = d.id and db.begin_time <= 1642931662 and (db.end_time - d.min_nights * 24 * 60 * 60 >= 1642931662) right join deals_languages as dl on dl.deal_id = d.id and dl.lang = 'bg' where d.end_time >= 1642931662 and d.begin_time <= 1642931662 and d.active='1' and d.deleted='0' and d.id!= 14757 and (dv.max_count = 0 or dv.max_count > dv.now_count) and d.country_id = '1' and IF(p.approval = 1, d.approval = '1', (d.approval = '1' or d.approval = '0')) and dcc.category_id in (11, 31 ) group by d.id order by rand(), d.end_time limit 6