I have the following query that selects most of the information I need:
SELECT cm.*, companies.company_name, companies.permalink,last_met.*,(cm.total_unique_visitors - last_met.last_UV)/last_met.last_UV * 100 as percent_change FROM calculated_metrics as cm
LEFT JOIN companies ON companies.company_id = cm.company_id
LEFT JOIN (SELECT company_id,total_unique_visitors as last_UV FROM calculated_metrics WHERE MONTH(date) = '04' AND YEAR(date) = '2011' GROUP BY company_id) last_met ON cm.company_id = last_met.company_id
WHERE MONTH(cm.date) = '05' AND YEAR(cm.date) = '2011'
AND cm.total_unique_visitors >3000
AND cm.total_unique_visitors<9999999
ORDER BY percent_change DESC
I have now been asked to sort the data even more. I need to sort the data by 2 tables(marketing_strategy and product_type). These tables both contain a company_id and multiple records for each company Example
marketing_strategy (table)
company_id marketing_strategy_option_id
605 25
605 9
604 21
604 9
product_type (table)
company_id product_type_option_id
605 12
605 13
604 13
604 3
What is the best way to sort using these 2 tables and the query above?
I will have a comma delimited string for the marketing_strategy options and the product_type options.
(Ex. marketing_strategy: 25,9 product_type: 13,3)
Would it be easier to run the query above loop through using PHP and then select only the companies that contain the specific marketing_strategy and product_type id's?
I am at a loss here.