Invalid query: (SELECT SQL_CALC_FOUND_ROWS tG.*, tGc.price AS price, tGc.price AS price_compare, tGc.availability AS availability, tGc.store_sum AS store_sum, tM.url AS model_url, tM.seasson AS seasson, tM.tenon AS tenon, tM.name AS model, tM.image AS image, tM.id AS model_id, tB.url AS brand_url, tB.name AS brand, tB.id AS brand_id, tB.url AS brand_url, tB.name AS brand_name, tB.image AS brand_image, IF(tGc.availability > 0 AND tGc.price > 0, 1, 0) AS availability_filter, tM.rating_avg AS rating, (CASE WHEN tG.popularity > 0 THEN tG.popularity WHEN tM.popularity > 0 THEN tM.popularity WHEN tB.popularity > 0 THEN tB.popularity ELSE 0 END) AS popularity_compare FROM catalog_tyres_goods tG, catalog_tyres_goods_cities tGc, catalog_tyres_models tM, catalog_tyres_brands tB WHERE tG.visibility AND tM.visibility AND tB.visibility AND ( (tGc.city = 1007 AND tGc.pid = tG.id AND tGc.price > 0 AND (tGc.availability > 0 OR tGc.store_sum > 0)) ) AND tG.pid = tM.id AND tM.pid = tB.id AND (tM.seasson = 1 OR tM.seasson = 0) ORDER BY availability_filter DESC) UNION (SELECT DISTINCT tG.*, tG.price AS price, tG.price AS price_compare, tG.availability AS availability, tG.store_sum AS store_sum, tM.url AS model_url, tM.seasson AS seasson, tM.tenon AS tenon, tM.name AS model, tM.image AS image, tM.id AS model_id, tB.url AS brand_url, tB.name AS brand, tB.id AS brand_id, tB.url AS brand_url, tB.name AS brand_name, tB.image AS brand_image, 0 AS availability_filter,tM.rating_avg AS rating, (CASE WHEN tG.popularity > 0 THEN tG.popularity WHEN tM.popularity > 0 THEN tM.popularity WHEN tB.popularity > 0 THEN tB.popularity ELSE 0 END) AS popularity_compare FROM catalog_tyres_goods tG, catalog_tyres_models tM, catalog_tyres_brands tB WHERE tG.visibility AND tM.visibility AND tB.visibility AND tG.pid = tM.id AND tM.pid = tB.id AND (tM.seasson = 1 OR tM.seasson = 0) AND ( ( (tG.id NOT IN (SELECT DISTINCT pid FROM catalog_tyres_goods_cities WHERE city = 1007)) OR (tG.id IN (SELECT DISTINCT pid FROM catalog_tyres_goods_cities WHERE city = 1007 AND (availability < 1 OR price < 1)))) AND tG.availability > 0) AND tG.price > 0 ) ORDER BY availability_filter DESC, rating DESC LIMIT 300,50 Lost connection to MySQL server during query