
- 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
Get the sum of last 3 digits from all the values in a column with MySQL
Since we want the sum of last 3 digits, we need to use aggregate function SUM() along with RIGHT(). Let us first create a table −
mysql> create table DemoTable ( Code int ); Query OK, 0 rows affected (0.77 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(5464322); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable values(90884); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(23455644); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values(4353633); Query OK, 1 row affected (0.11 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+----------+ | Code | +----------+ | 5464322 | | 90884 | | 23455644 | | 4353633 | +----------+ 4 rows in set (0.00 sec)
Following is the query to get the sum of last 3 digits of all the values in a column −
mysql> select sum(right(Code,3)) AS SumOfLast3Digit from DemoTable;
This will produce the following output −
+-----------------+ | SumOfLast3Digit | +-----------------+ | 2483 | +-----------------+ 1 row in set (0.00 sec)
- Related Articles
- Get the sum of a column in all MySQL rows?
- Get the sum of multiple row (not all) values from a MySQL table?
- Get all the records with two different values in another column with MySQL
- How to get last 12 digits from a string in MySQL?
- Get the count of duplicate values from a single column in MySQL?
- How to subtract the same amount from all values in a column with MySQL?
- MySQL query to exclude values having specific last 3 digits
- Display the sum of positive and negative values from a column in separate columns with MySQL
- Sum the values in a column in MySQL?
- Get sum of column with conditions in MySQL
- MySQL query to get the character length for all the values in a column?
- Get the last days of all the months in MySQL?
- Get the maximum count of distinct values in a separate column with MySQL
- Can I get the count of repeated values in a column with MySQL?
- Get the last record from a table in MySQL database with Java?

Advertisements