You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '238) and (((inv.state= 1 and now() > (inv.start_date) and (now() < (inv.star' at line 31 SQL=select slec.*, mainCategory, mainCategoryId, categoryMaker, companyName, features, GREATEST(if(FIND_IN_SET('featured_companies',features) ,1,0), featured) as featured from ( select cp.name as companyName, cp.id as companyId1, cp.id, cp.name, cp.alias, cp.short_description, cp.meta_description, cp.street_number, cp.address, cp.city, cp.county, cp.province, cp.area, cp.website, cp.phone, cp.mobile, cp.email, cp.state, cp.fax, cp.averageRating, cp.slogan,cp.logoLocation, cp.activity_radius, cp. review_score, cp.creationDate, cp.featured,cp.publish_only_city,cp.userId, cp.latitude, cp.longitude, cp.keywords,cp.approved, cp.mainSubcategory, cp.countryId,cp.typeId, cp.postalCode, cp.publish_start_date, cp.publish_end_date, GROUP_CONCAT( DISTINCT cg.id,'|',cg.name,'|',cg.alias ORDER BY cg.name separator '#|') as categories, GROUP_CONCAT( DISTINCT cg.id) as categoryIds, bcm.name as mainCategory, bcm.alias as mainCategoryAlias ,bcm.markerLocation as categoryMaker, bcm.id as mainCategoryId, ct.name as typeName, #cnt.contact_name, count(distinct cra.id) as nrRatings, GROUP_CONCAT(DISTINCT pf.feature) as features, max(p.ordering) as packageOrder from #__jbusinessdirectory_companies cp left join #__jbusinessdirectory_company_category cc on cp.id=cc.companyId left join #__jbusinessdirectory_categories cg on cg.id=cc.categoryId and cg.published=1 left join #__jbusinessdirectory_categories bcm on bcm.id=cp.mainSubcategory and bcm.published=1 #left join #__jbusinessdirectory_company_contact cnt on cp.id=cnt.companyId left join #__jbusinessdirectory_company_types ct on cp.typeId=ct.id left join #__jbusinessdirectory_orders inv on inv.company_id=cp.id left join #__jbusinessdirectory_packages p on (inv.package_id=p.id and p.status=1 and 1) or (p.price=0 and p.status=1 and 1) left join #__jbusinessdirectory_package_fields pf on p.id=pf.package_id left join #__jbusinessdirectory_company_ratings cra on cra.companyId=cp.id where 1 and cp.state=1 and cc.categoryId in (,238) and (((inv.state= 1 and now() > (inv.start_date) and (now() < (inv.start_date + INTERVAL p.days DAY) or (inv.package_id=p.id and p.days = 0)))) or p.price=0) and (cp.approved = 2 or cp.approved= -1 or cp.approved= 0) and (cp.publish_start_date<=DATE(now()) or cp.publish_start_date='0000-00-00' or cp.publish_start_date is null) and (cp.publish_end_date>=DATE(now()) or cp.publish_end_date='0000-00-00' or cp.publish_end_date is null) group by cp.id ) as slec where 1 having 1 order by featured desc, companyName LIMIT 0, 50