0

I need to create table where first column is names of clients and next ones are months of previous year starting from current month with amount of orders of user in this month. So my problem is that I don't really know how to name columns as months starting from current. I know that i can get name of month with namemonth() but i can't use it as name of column. Any ideas?

FtD
  • 1
  • 1
    Does this answer your question? [MySQL - Rows to Columns](https://stackoverflow.com/questions/1241178/mysql-rows-to-columns) – Progman May 03 '20 at 09:45
  • Its recommended you handle the formatting of the months in an application language rather than using complex SQL to pivot your data. – danblack May 03 '20 at 12:06

1 Answers1

0

If I understand it right you want to dynamically name your columns. If you really want to do that you can write a script for that in your favorite programming language. But I don't recommend that you do that as it's not a good practive.

This is what I would do:

Create a table with 3 columns, the first one is the name or id of the client (of type int or varchar), the second one the date (of type date) and the third one the value (of type int as an amount of orders can only be a whole number). Then you can write a script to get the amount of orders for a specific (or all) client(s) with a script based on the date.

But if you really want to do what you said, which I really advise against, have a look at the link provieded by Progman in his comment.

Timen
  • 56
  • 1
  • 7