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
Ubuntu is a free and open source desktop PC opera...
Table of contents Why is addEventListener needed?...
1. Overall steps At the beginning, we introduced ...
This article shares the specific code of JS to ac...
Application example website http://www.uhuigou.net...
Preface In WEB development, we often involve cros...
Preface During project development, due to differ...
This article shares the installation and configur...
CentOS6.9+Mysql5.7.18 source code installation, t...
Project scenario: 1. Upload file restrictions Fun...
Table of contents Overview Functionality and read...
Nginx: PV, UV, independent IP Everyone who makes ...
Table of contents Preface Introduction to Bezier ...
When the user's home directory becomes larger...
1. Concat function. Commonly used connection stri...