
- 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 sum a comma separated string (string with numbers) in MySQL?
You can create a custom function to sum a comma-separated string in MySQL. Let us first create a table. Here, we have a varchar column, wherein we will add numbers in the form of strings −
mysql> create table DemoTable -> ( -> ListOfValues varchar(50) -> ); Query OK, 0 rows affected (0.56 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('20,10,40,50,60'); Query OK, 1 row affected (0.14 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+----------------+ | ListOfValues | +----------------+ | 20,10,40,50,60 | +----------------+ 1 row in set (0.00 sec)
Here is the query to create a function −
mysql> DELIMITER ?? mysql> create function totalSumInCommaSeparatedString(input varchar(50)) -> returns int -> deterministic -> no sql -> begin -> declare totalSum int default 0; -> while instr(input, ",") > 0 do -> set totalSum = totalSum + substring_index(input, ",", 1); -> set input = mid(input, instr(input, ",") + 1); -> end while; -> return totalSum + input; -> end ?? Query OK, 0 rows affected (0.17 sec) mysql> DELIMITER ;
Let us check the above function to get some of a comma-separated string in MySQL −
mysql> select totalSumInCommaSeparatedString(ListOfValues) as TotalSum from DemoTable;
This will produce the following output −
+----------+ | TotalSum | +----------+ | 180 | +----------+ 1 row in set (0.00 sec)
- Related Articles
- Remove specific word in a comma separated string with MySQL
- How to use a comma-separated string in an `IN ()` in MySQL?
- Convert a List of String to a comma separated String in Java
- Convert a Set of String to a comma separated String in Java
- How to create a comma separated string from a list of string in C#?
- Regex to find string and next character in a comma separated list - MySQL?
- Java Program to Convert a List of String to Comma Separated String
- MySQL query to get a single value from position of comma-separated string?
- How to convert a comma separated String into an ArrayList in Java?
- Convert String into comma separated List in Java
- How to include quotes in comma separated column with MySQL?
- How would you make a comma-separated string from a list in Python?
- How to fetch random rows in MySQL with comma separated values?
- C++ Program to take out integer from comma separated string
- How to split comma and semicolon separated string into a two-dimensional array in JavaScript ?

Advertisements