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:
|
<<: How to use ES6 class inheritance to achieve a gorgeous ball effect
>>: How to install ELK in Docker and implement JSON format log analysis
When the front-end requests the interface, it is ...
Table of contents MutationObserver API Features I...
1. Download from the official website and unzip h...
In development, it is often necessary to cache th...
This article shares the specific code for JavaScr...
Table of contents 1. Concept 2. Environmental Des...
All prerequisites require root permissions 1. End...
Record the problem points of MySQL production. Bu...
Preface The SQL mode affects the SQL syntax that ...
This article shares the specific code of js to re...
In HTML and CSS, we want to set the color of a bu...
Time flies, and in just six days, 2013 will becom...
The docker image id is unique and can physically ...
I want to make a page using CSS3 rounded corners ...
Table of contents Undo Log Undo Log Generation an...