Possible Duplicate:
MySQL pivot row into dynamic number of columns
I have a query that shows number of items per day
$query = "SELECT uploadedby, count(item) as item, date_format(uploaddate, '%m-%d-%y') as date FROM `imsexport` GROUP BY uploadedby,date";
$result = mysql_query($query);
The resulting table looks like this
Name item date
Abel Antonio 10 01-02-12
Abel Antonio 5 01-03-12
Abel Antonio 6 01-04-12
Abel Antonio 2 01-05-12
Abel Antonio 1 01-09-12
Abel Antonio 15 01-12-12
Abel Antonio 22 01-16-12
Abel Antonio 15 01-17-12
Abel Antonio 7 01-19-12
Abel Antonio 45 02-15-12
Abel Antonio 31 02-16-12
...other names
I’d like to make the the dates as columns to make the query look like this
Name 01-02-12 01-03-12 01-04-12 01-05-12 01-09-12 ....
Abel Antonio 10 5 6 2 1 .....
Anyone know how to accomplish this?
So I think you would like to do a Pivot using a Dynamic number of columns. It depends what database you are using, but I’m guessing by the date_format statement that it is MYSQL. Here is a link to an answer that does what you want – enjoy!
https://stackoverflow.com/a/12005676/345659
Your code should look like this:
I’ve used this data in the Fiddle provided by bluefeet in the linked question: