SELECT
cscart_companies.company_id,
cscart_companies.lang_code,
cscart_companies.email,
cscart_companies.company,
cscart_companies.timestamp,
cscart_companies.status,
cscart_companies.tax_number,
cscart_company_descriptions.company_description,
absolute_rating.rating AS absolute_vendor_rating,
cscart_vendor_plan_descriptions.plan,
cscart_seo_names.name as seo_name,
cscart_seo_names.path as seo_path,
AVG(
cscart_discussion_rating.rating_value
) AS average_rating,
CONCAT(
cscart_companies.company_id,
'_',
IF (
cscart_discussion_rating.thread_id,
cscart_discussion_rating.thread_id,
'0'
)
) AS company_thread_ids,
cscart_companies.suspend_date,
cscart_companies.last_time_suspended
FROM
cscart_companies
LEFT JOIN cscart_company_descriptions ON cscart_company_descriptions.company_id = cscart_companies.company_id
AND cscart_company_descriptions.lang_code = 'tr'
LEFT JOIN cscart_absolute_rating AS absolute_rating ON absolute_rating.object_id = cscart_companies.company_id
AND absolute_rating.object_type = 'company'
LEFT JOIN cscart_vendor_plan_descriptions ON cscart_companies.plan_id = cscart_vendor_plan_descriptions.plan_id
AND cscart_vendor_plan_descriptions.lang_code = 'tr'
LEFT JOIN cscart_seo_names ON cscart_seo_names.object_id = cscart_companies.company_id
AND cscart_seo_names.type = 'm'
AND cscart_seo_names.dispatch = ''
AND cscart_seo_names.lang_code = 'tr'
LEFT JOIN cscart_discussion ON cscart_discussion.object_id = cscart_companies.company_id
AND cscart_discussion.object_type = 'M'
LEFT JOIN cscart_discussion_posts ON cscart_discussion_posts.thread_id = cscart_discussion.thread_id
AND cscart_discussion_posts.status = 'A'
LEFT JOIN cscart_discussion_rating ON cscart_discussion.thread_id = cscart_discussion_rating.thread_id
AND cscart_discussion_rating.post_id = cscart_discussion_posts.post_id
WHERE
1
AND cscart_companies.status = 'A'
AND cscart_companies.company_id IN (3, 4)
GROUP BY
company_thread_ids
ORDER BY
cscart_companies.company asc
LIMIT
0, 10