0

I have create a table in MySQL where i have all the names of data I'm going to bring in from FRED data.

enter image description here

Now I want to make a new table, where the 1504 names in my example table above, each has one column.

AAA AAA10M AAAFFM ADBJORNS and so on.

So every name in fred_namecol should get one column each with numeric as value. Is there a easy way to do this instead of writing everyone manually?

Maybe there is a way too loop trough each name and make a column for them into a new table?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
A.Salehi
  • 33
  • 8
  • 1
    Bad idea.. MySQL has a max column limit in tables so it will fail with too many records if somebody provide a "solution".. "MySQL has hard limit of 4096 columns per table, but the effective maximum may be less for a given table. The exact column limit depends on several factors: " source https://dev.mysql.com/doc/refman/5.7/en/column-count-limit.html – Raymond Nijland Jun 24 '18 at 19:32
  • You have a table with your users (1504 of them). You want to keep on record a numerical value for each user right? So just add a new column in your users table with a numerical type and store the number there. No need for another table. Do it at the row, not the column. If your model forces such a setup, I contend that your model is wrong. – Nic3500 Jun 24 '18 at 19:55
  • I will get 1504 columns, and the data is time series, so I will insert a date column later. – A.Salehi Jun 25 '18 at 19:02

1 Answers1

0

Yes. There is a way.

In the following query; replace WRITE_YOUR_TABLE_NAME_HERE with the name of the table which contains fred_namecol column:

drop table if exists columns_over_1000;
set @prefix = 'CREATE TABLE columns_over_1000 (';
set @suffix = '\n);';
select @create_table_sql := concat(
    @prefix, 
        group_concat('\n    `', fred_namecol, '` INT'), 
    @suffix) 
from `WRITE_YOUR_TABLE_NAME_HERE`;

PREPARE stmt1 FROM @create_table_sql;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;

select * from columns_over_1000;
pegasuspect
  • 991
  • 4
  • 15
  • This code almost works i get the Idea, thanks but I get an error not sure why, "ERROR Code: 1064. You have an error in your SQL syntax; check the manual to your MySQL server version." The error comes up at prepare stmt1. But i get the idea, i get a text with all the column names. – A.Salehi Jun 25 '18 at 19:26
  • @A.Salehi Yes, PREPARE is actually setting that text to a variable called stmt1 and executing it. You can remove the lines after and including PREPARE and simply copy and paste the generated sql from the results. – pegasuspect Jun 25 '18 at 22:10
  • Is that all it says about the error? It is very brief to understand the reason. If not, could you please write the whole error here. – pegasuspect Jun 25 '18 at 22:26
  • Yeah its, all it says. Weird cant find why, just copied straight from the code. I tried to copy and paste the genereted SQL text but it wasnt all 1504 names from the list (I only get like first 52 names). I try copy row and all other options for copying, to copy it into a new script. When i open value in viewer samething, not all of them are there. – A.Salehi Jun 26 '18 at 18:51
  • The Type is MEDIUMTEXT maybe thats why? I Found this "https://stackoverflow.com/questions/31658760/group-concat-and-longtext" "I believe you should be able to set it just for the session (current connection) without needing to change it globally/permanently on the server. Executing something like SET group_concat_max_len = 1000000; before your query should solve the issue." SOLVED NOW I CAN JUST COPY IT TO A NEW SCRIPT! THANKS FOR THE HELP!! – A.Salehi Jun 26 '18 at 19:02