Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Mysql varchar type summation instance operation

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

When some friends are learning the database, when creating the table structure, they accidentally set a field to varchar, but they are dumbfounded at the time of statistical summation. Next, follow the editor to learn that you do not have to change the column data type to sum it.

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

2. After you have successfully created the table, create some test data for the table you just made, as shown below:

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

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

5, then take a look at the CONVERT () function, which mainly serves the requirement of precision. Here, change the original data and change the age column to one with decimals 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 summation of varchar types also gives a solution!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report