
- 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
How to add column values in MySQL without using aggregate function?
You can add column values without using aggregate function like sum(). For that, the syntax is as follows −
SELECT *,(yourColumnName1+yourColumnName2+yourColumnName3,....N) as anyVariableName from yourTableName;
To understand the above syntax, let us create a table. The query to create a table is as follows −
mysql> create table AddingColumnDemo -> ( -> StudentId int, -> StudentName varchar(20), -> MathMarks int, -> PhysicsMarks int, -> ChemistryMarks int -> ); Query OK, 0 rows affected (0.82 sec)
Insert records in the table using insert command. The query is as follows −
mysql> insert into AddingColumnDemo values(1,'John',35,45,76); Query OK, 1 row affected (0.25 sec) mysql> insert into AddingColumnDemo values(2,'Bob',67,76,88); Query OK, 1 row affected (0.19 sec) mysql> insert into AddingColumnDemo values(3,'Carol',45,56,43); Query OK, 1 row affected (0.16 sec) mysql> insert into AddingColumnDemo values(4,'Mike',82,75,71); Query OK, 1 row affected (0.21 sec) mysql> insert into AddingColumnDemo values(5,'Sam',92,89,88); Query OK, 1 row affected (0.23 sec)
Display all records from the table using select statement −
mysql> select *from AddingColumnDemo;
The following is the output displaying the records of the table −
+-----------+-------------+-----------+--------------+----------------+ | StudentId | StudentName | MathMarks | PhysicsMarks | ChemistryMarks | +-----------+-------------+-----------+--------------+----------------+ | 1 | John | 35 | 45 | 76 | | 2 | Bob | 67 | 76 | 88 | | 3 | Carol | 45 | 56 | 43 | | 4 | Mike | 82 | 75 | 71 | | 5 | Sam | 92 | 89 | 88 | +-----------+-------------+-----------+--------------+----------------+ 5 rows in set (0.00 sec)
Let us now implement the query to add column values in MySQL −
mysql> select *,(MathMarks+PhysicsMarks+ChemistryMarks) as TotalResult from AddingColumnDemo;
The following is the output displaying the sum of column values in TotalResult column −
+-----------+-------------+-----------+--------------+----------------+-------------+ | StudentId | StudentName | MathMarks | PhysicsMarks | ChemistryMarks | TotalResult | +-----------+-------------+-----------+--------------+----------------+-------------+ | 1 | John | 35 | 45 | 76 | 156 | | 2 | Bob | 67 | 76 | 88 | 231 | | 3 | Carol | 45 | 56 | 43 | 144 | | 4 | Mike | 82 | 75 | 71 | 228 | | 5 | Sam | 92 | 89 | 88 | 269 | +-----------+-------------+-----------+--------------+----------------+-------------+ 5 rows in set (0.00 sec)
- Related Articles
- Find the average of column values in MySQL using aggregate function
- Add to existing value in MySQL column using CONCAT function?
- Add 6 hours to now() function without using DATE_ADD() in MySQL?
- How to add duplicate varchar values without displaying error in MySQL?
- Using Aggregate function to fetch values from different tables in SAP
- How to add column using alter in MySQL?\n
- MySQL SUM function to add decimal values
- How to add comment to column in MySQL using Python?
- How to change the column names in R within aggregate function?
- Get the maximum value of a column with MySQL Aggregate function
- How to add a leading zero to some values in a column in MySQL?
- How will GROUP BY clause perform without an aggregate function?
- Set particular value of column without using update and while inserting values in MySQL
- How to add auto-increment to column in MySQL database using PhpMyAdmin?
- How can column data values of a table be compared using MySQL STRCMP() function?

Advertisements