Mysql varchar type sum example operation

Mysql varchar type sum example operation

Some friends, when learning about databases, accidentally set a field to varchar when creating the table structure, but were dumbfounded when they tried to sum the data. Next, follow the editor to learn how to sum the data without changing the data type of the column!

1. Open the database connection client Navicat Premium and create a new table structure. Here, the age column is intentionally set to varchar.

2. After the table is created successfully, create some test data for the table just now, as shown below:

3. When the amount of data is small, you can use the sum() function to directly sum it, because in MySQL it can automatically identify whether it is a string type or a numeric type.

4. The above is applicable to integers or small amounts of data. If the amount of data is too large and high precision is not applicable, you can consider using MySQL's CAST() and CONVERT() functions. As shown below:

5. Next, let’s look at the CONVERT() function. This function mainly serves the needs of precision. Here, we change the original data and change the age column to a decimal to test this function.

6. The above two functions support the following types of conversion, as shown in the following figure:

At this point, the sum of varchar types also provides a solution!

You may also be interested in:
  • Method for comparing the size of varchar type numbers in MySQL database
  • Some things to note about varchar type in Mysql
  • The difference between char, varchar and text field types in MySQL
  • MySQL data type varchar detailed explanation
  • Implementation of comparison, sorting and other operations on varchar type dates in MySQL

<<:  How to use ES6 class inheritance to achieve a gorgeous ball effect

>>:  How to install ELK in Docker and implement JSON format log analysis

Recommend

Detailed explanation of uniapp painless token refresh method

When the front-end requests the interface, it is ...

Detailed explanation of keepAlive use cases in Vue

In development, it is often necessary to cache th...

JavaScript to achieve fireworks effects (object-oriented)

This article shares the specific code for JavaScr...

MySQL dual-machine hot standby implementation solution [testable]

Table of contents 1. Concept 2. Environmental Des...

Linux/Mac MySQL forgotten password command line method to change the password

All prerequisites require root permissions 1. End...

Detailed explanation of MySQL deadlock and database and table sharding issues

Record the problem points of MySQL production. Bu...

Summary of the characteristics of SQL mode in MySQL

Preface The SQL mode affects the SQL syntax that ...

js implements mouse in and out card switching content

This article shares the specific code of js to re...

jQuery implements simple button color change

In HTML and CSS, we want to set the color of a bu...

The hottest trends in web design UI in 2013 The most popular UI designs

Time flies, and in just six days, 2013 will becom...

Docker implements re-tagging and deleting the image of the original tag

The docker image id is unique and can physically ...

Let IE6, IE7, IE8 support CSS3 rounded corners and shadow styles

I want to make a page using CSS3 rounded corners ...

Summary of MySQL Undo Log and Redo Log

Table of contents Undo Log Undo Log Generation an...