简体   繁体   中英

Add new column to table and fill with the data of 2 other columns?

I have a table with two columns: ColumnA and ColumnB .

How can I add a third column to my table that is ColumnA divided by ColumnB ?

I know this seems simple, but I have been unable to find the syntax.

You mean something like this:

SELECT ColumnA, 
       ColumnB, 
       (ColumnA / ColumnB) as ColumnC 
  FROM myTable

or to actually add a new column and fill it with the result of ColumnA divided by ColumnB like this:

ALTER TABLE `myTable` ADD `ColumnC` INT NOT NULL;
UPDATE `myTable` SET ColumnC = (ColumnA / ColumnB);

You can see more options about the ALTER TABLE syntax here.

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