My ultimate goal is to create a pivot table view in MySQL with dynamic columns based on the contents of another table. At the moment, I am trying to continue when artfulsoftware is not working; right now I can query for the results that give me the desired column names. Unfortunately, I lost information on how to use the results as column names in a SELECT statement. I suspect MySQL variables will be useful, but I can't figure it out.
To clarify the problem, let's say I have a table like:
+---------------------------------------------------+ | countpivotarg | +---------------------------------------------------+ | ,SUM(IF(domain = "test.com",1,0)) AS `test.com` | | ,SUM(IF(domain = "test2.com",1,0)) AS `test2.com` | +---------------------------------------------------+
I want to create a select statement that looks like this:
SELECT id, meta_id, SUM(IF(domain = "test.com",1,0)) AS `test.com`, SUM(IF(domain = "test2.com",1,0)) AS `test2.com` FROM myTable;
How can I do it?
source share