SELECT ***
FROM (
SELECT m.powertrain_id AS id, *COUNT*(***) AS select_count
FROM SALES s
JOIN MODEL m ON s.model_id = m.id
WHERE (s.tag1 = 3 OR s.tag2 = 3 OR s.tag3 = 3)
GROUP BY m.powertrain_id WITH ROLLUP
) AS count_table
WHERE id = 1 OR id IS NULL;
SELECT *
FROM (
SELECT so.additional_option_id AS id, COUNT(*) AS select_count
FROM SALES_OPTIONS so
WHERE so.sales_id IN
(SELECT id FROM SALES
WHERE 9 IN (tag1, tag2, tag3)
AND model_id IN
(SELECT id FROM MODEL WHERE trim_id = 1)
)
GROUP BY so.additional_option_id WITH ROLLUP
) AS count_table
WHERE id = 130 OR id IS NULL;
SELECT *
FROM (
SELECT so.additional_option_id AS id, COUNT(*) AS select_count
FROM MODEL m
INNER JOIN SALES s
ON s.model_id = m.id
INNER JOIN SALES_OPTIONS so
ON s.id = so.sales_id
WHERE m.trim_id = 1 AND 9 IN (s.tag1, s.tag2, s.tag3)
GROUP BY so.additional_option_id WITH ROLLUP
) AS count_table
WHERE id = 130 OR id IS NULL;