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

How to install MySQL database on Ubuntu

Ubuntu is a free and open source desktop PC opera...

Detailed explanation of the difference between JavaScript onclick and click

Table of contents Why is addEventListener needed?...

Linux RabbitMQ cluster construction process diagram

1. Overall steps At the beginning, we introduced ...

JS realizes the effect of Baidu News navigation bar

This article shares the specific code of JS to ac...

CSS specification BEM CSS and OOCSS sample code detailed explanation

Preface During project development, due to differ...

CentOS6.9+Mysql5.7.18 source code installation detailed tutorial

CentOS6.9+Mysql5.7.18 source code installation, t...

Detailed explanation of JavaScript upload file limit parameter case

Project scenario: 1. Upload file restrictions Fun...

Deep understanding of JavaScript syntax and code structure

Table of contents Overview Functionality and read...

How to use Javascript to generate smooth curves

Table of contents Preface Introduction to Bezier ...

Summary of MySQL commonly used type conversion functions (recommended)

1. Concat function. Commonly used connection stri...