
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
Add a new column to table and fill it with the data of two other columns of the same table in MySQL?
Let us first create a table −
mysql> create table DemoTable ( Price int, Quantity int ); Query OK, 0 rows affected (0.71 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(45,3); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(90,2); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values(440,1); Query OK, 1 row affected (0.09 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-------+----------+ | Price | Quantity | +-------+----------+ | 45 | 3 | | 90 | 2 | | 440 | 1 | +-------+----------+ 3 rows in set (0.00 sec)
Following is the query to add a new column to the table and fill with the data of 2 other columns. Here, we have added new column TotalAmount and the data in it is the multiplication of the values from the first two columns −
mysql> select Price,Quantity, Price*Quantity AS TotalAmount from DemoTable;
This will produce the following output −
+-------+----------+-------------+ | Price | Quantity | TotalAmount | +-------+----------+-------------+ | 45 | 3 | 135 | | 90 | 2 | 180 | | 440 | 1 | 440 | +-------+----------+-------------+ 3 rows in set (0.00 sec)
- Related Articles
- Add new MySQL table columns and create indexes?
- Multiply values of two columns and display it a new column in MySQL?
- Select and add result of multiplying two columns from a table in MySQL?
- How to create and fill a new column in an already created MySQL table?
- Add a new column and index to an existing table with ALTER in a single MySQL query?
- Inserting data into a new column of an already existing table in MySQL?
- MySQL query to count the number of 0s and 1s from a table column and display them in two columns?
- How can we add values into the columns of a MySQL table?
- How to add a new column Address in the above DB2 table TAB1?
- Select some data from a database table and insert into another table in the same database with MySQL
- Count the number of columns in a MySQL table with Java
- How to repeat the values stored in a data column of MySQL table?
- Can we add a column to a table from another table in MySQL?
- Is it possible to have View and table with the same name in MySQL?
- How to add a new column in an R data frame by combining two columns with a special character?

Advertisements