Reputation: 41
In MySQL, Is it possible to set dates as column name.
I am thinking about to create a table with last 3 week data where column names of all three weeks will be the first date of that week.
So if currante date is 25-09-2013 then my table should have 3 columns with column name as:
11-09-2013,28-09-2013 and 25-09-2013.
Upvotes: 0
Views: 2258
Reputation: 2588
You want to do that in the display layer.
select columns as
if(date=curdate() - interval 1 day, result, null) as today_minus_1,
if(date=curdate() - interval 2 day, result, null) as today_minus_2,
if(date=curdate() - interval 2 day, result, null) as today_minus_3,
afterwards when you are displaying, rename them. I did something like that before to compare performance of campaigns, but column naming as date was done when creating the data table.
Upvotes: 0
Reputation: 9209
What is your justification for doing this?
If it is just so that the data looks more readable then that can be achieved by running a report that formats the data into the layout that you require.
Just store your data with a date tag on each entry, then retrieve it in 3 week blocks within your report.
Creating tables on the fly will add unnecessary overheads to your processing and complicate backup, replication and retrieval by other processes.
Upvotes: 0
Reputation: 308823
It might be possible, but I'd say it's a terrible, denormalized, unsustainable, brain dead design. I'd recommend rethinking it, knowing nothing about your requirements.
Upvotes: 1
Reputation: 37365
Sure, why not:
mysql> select 'foo' as `2013-09-25`; +------------+ | 2013-09-25 | +------------+ | foo | +------------+ 1 row in set (0.00 sec)
-just remember about backticks.
But, actually, if you want to produce some dynamic columns that is not a good idea - you'd better to use application for that.
Upvotes: 3