Fetch the size of a specific column values in MySQL and display the sum


Let us first create a table −

mysql> create table DemoTable1612
   -> (
   -> FirstName varchar(20),
   -> LastName varchar(20)
   -> );
Query OK, 0 rows affected (0.87 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1612 values('David','Brown');
Query OK, 1 row affected (0.75 sec)
mysql> insert into DemoTable1612 values('John','Smith');
Query OK, 1 row affected (0.21 sec)
mysql> insert into DemoTable1612 values('Bob','Taylor');
Query OK, 1 row affected (0.13 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1612;

This will produce the following output −

+-----------+----------+
| FirstName | LastName |
+-----------+----------+
| David     |    Brown |
| John      |    Smith |
| Bob       |   Taylor |
+-----------+----------+
3 rows in set (0.00 sec)

Following is the query to get the size of selected rows in MySQL −

mysql> select sum(char_length(FirstName)) from DemoTable1612 where LastName IN('Brown','Smith','Taylor');

This will produce the following output −

+-----------------------------+
| sum(char_length(FirstName)) |
+-----------------------------+
|                          12 |
+-----------------------------+
1 row in set (0.00 sec)

Updated on: 10-Dec-2019

74 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements