简体   繁体   中英

Add dynamic column to existing MySQL table?

just wondering how to insert dynamic column to existing MySQL table? For example: I already have " sampletable " and I want to make input fields that can add dynamic column to the existing table , example: column1, column2, column3 . How to do that with dynamic numbering?

I would agree with @Barmar that your SQL table structure is wrong if you are trying to do this. What you are trying to do in this case is what's called a "one to many" relationship. This is usually achieved by doing something like the following.

Table 1: Contains columns for all the usual data (non-"dynamic" columns in your terms), and a unique ID column which all good database tables should have

Table 2: An ID column, and column that refers to the ID column on table one and a column for the data that goes in the dynamic column.

Now you can store your values that you would normally store in "dynamic columns" in individual rows on the second table.

Example

//  sample:
//
// | id | name |
//
// dynamic_values:
//
// | id | sample_id | value |

// Selecting data

SELECT * FROM sample WHERE id = 1;
SELECT * FROM dynamic_values WHERE sample_id = 1;

// Querying on "dynamic columns"
SELECT * FROM sample s LEFT JOIN dynamic_values d ON d.sample_id = s.id WHERE d.value = 'something';

Try This set of code for Dynamic Column Creation for Existing Table.

SET SQL_SAFE_UPDATES = 0;
        Drop TEMPORARY table if exists Temp_Report;
        CREATE TEMPORARY TABLE Temp_Report (Report_Date Date);

        Drop TEMPORARY table if exists Temp_Product_Tax;
        CREATE TEMPORARY TABLE Temp_Product_Tax as SELECT concat(REPLACE(Tax_category,' ','_'),'|',Taxvalue) as 'Tax_category',Taxvalue FROM tax_category c left join taxmaster t on c.id=t.catid ; -- where c.is_Product =1

    select * from Temp_Product_Tax;
            set Count_1=(SELECT COUNT(*) FROM Temp_Product_Tax);
                set Var_1=0;     
                    While(Count_1>Var_1) do
                            set @Col_Name=Concat( Var_1+1,'_',REPLACE((select Tax_category from Temp_Product_Tax limit Var_1,1),'.','_'),' Double(15,2)');

                            set @Col_Name=Concat('ALTER TABLE Temp_Report ADD COLUMN ', @Col_Name) ;
                            PREPARE stmt FROM @Col_Name;
                            EXECUTE stmt;

                    set Var_1=Var_1+1;
                    END While;  

    select * from Temp_Report;

    SET SQL_SAFE_UPDATES = 1;

In fact, what you intend to do, ie, adding dynamic columns is not at all a good practice I think. Anyway You can do that using ALTER TABLE

for($i=1;$i<4;$i++){

    mysqli_query("ALTER TABLE mytable ADD COLUMN `input.$i` VARCHAR(40)",$db_con);
}

But I would suggest the same way, which is BARMER mentioned in the above comments.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM