1. Introduction The following two types are often used in field calculations:
2. Main text All field combinations implemented in 2.1 Field concatenationPrepare a user table and insert several records as follows: SET NAMES utf8mb4; SET FOREIGN_KEY_CHECKS = 0; -- ---------------------------- -- Table structure for user -- ---------------------------- DROP TABLE IF EXISTS `user`; CREATE TABLE `user` ( `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'Primary key', `name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT 'User name', `nation` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT 'Ethnicity', PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 9 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic; -- ---------------------------- -- Records of user -- ---------------------------- INSERT INTO `user` VALUES (1, '李子八', '汉族'); INSERT INTO `user` VALUES (2, '张三', '慧族'); INSERT INTO `user` VALUES (3, '李四', 'Uyghur'); INSERT INTO `user` VALUES (4, '王五', '蒙古'); SET FOREIGN_KEY_CHECKS = 1; need: Get the user's name and ethnic combination information Statement: mysql> select concat(name, '(',nation, ')') from user; +---------------------------------+ | concat(name, '(',nation, ')') | +---------------------------------+ | Li Ziba (Han nationality) | | Zhang San (Hui) | | Li Si (Uyghur) | | Wang Wu (Mongolian) | +---------------------------------+ Analysis: The What about the field name after combination? Careful friends found that the field name after combination uses the entire function body of mysql> select concat(name, '(',nation, ')') as user_message from user; +------------------+ | user_message | +------------------+ | Li Ziba (Han nationality) | | Zhang San (Hui) | | Li Si (Uyghur) | | Wang Wu (Mongolian) | +------------------+ The usage of an alias is to use 2.2 Performing Arithmetic Calculations on Fields When combining fields, we often do more than simply concatenate strings. It may involve arithmetic calculations between fields. In this case, we need to use arithmetic operators in MySQL provides addition, subtraction, multiplication and division operators as follows:
Prepare a product table and insert several records as follows: SET NAMES utf8mb4; SET FOREIGN_KEY_CHECKS = 0; -- ---------------------------- -- Table structure for product -- ---------------------------- DROP TABLE IF EXISTS `product`; CREATE TABLE `product` ( `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'Primary key', `product_name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT 'Product name', `price` decimal(10, 2) UNSIGNED NOT NULL COMMENT 'Product price', `number` int(11) NOT NULL COMMENT 'Product quantity', PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 7 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic; -- ---------------------------- -- Records of product -- ---------------------------- INSERT INTO `product` VALUES (1, 'Apple iPhone 13 (A2634)', 6799.00, 22); INSERT INTO `product` VALUES (2, 'HUAWEI P50 Pro', 6488.00, 88); INSERT INTO `product` VALUES (3, 'MIX4', 4999.00, 30); INSERT INTO `product` VALUES (4, 'OPPO Find X3', 3999.00, 15); INSERT INTO `product` VALUES (5, 'vivo X70 Pro+', 5999.00, 27); SET FOREIGN_KEY_CHECKS = 1; need: Query the total value of current inventory products Statement: mysql> select product_name, concat(price * number) as gross_value from product; +-------------------------+-------------+ | product_name | gross_value | +-------------------------+-------------+ | Apple iPhone 13 (A2634) | 149578.00 | | HUAWEI P50 Pro | 570944.00 | | MIX4 | 149970.00 | | OPPO Find X3 | 59985.00 | | vivo X70 Pro+ | 161973.00 | +-------------------------+-------------+ Operator order problem: Operators in Example: mysql> select concat(12 - 3 * 4); +--------------------+ | concat(12 - 3 * 4) | +--------------------+ | 0 | +--------------------+ 1 row in set (0.00 sec) mysql> select concat((12 - 3) * 4); +----------------------+ | concat((12 - 3) * 4) | +----------------------+ | 36 | +----------------------+ 1 row in set (0.00 sec) It is worth noting that in mysql> select concat(12 / 0); +----------------+ | concat(12 / 0) | +----------------+ | NULL | +----------------+ 1 row in set, 1 warning (0.00 sec) This is the end of this article about You may also be interested in:
|
<<: Summary of standard usage of html, css and js comments
>>: Ubuntu compiles kernel modules, and the content is reflected in the system log
This article shares the specific method of instal...
We often encounter this situation when doing devel...
Preface Sometimes file copies amount to a huge wa...
1. After connecting and logging in to MySQL, firs...
Preface: In the daily use of the database, it is ...
Table of contents 1. What is reflection? 2. Refle...
Let’s install Nginx and try it out. Please note t...
After installing a centos8 service under vmware a...
1. Install and use Docer CE This article takes Ce...
Table of contents 1. World Map 1. Install openlay...
Table of contents Common payment methods in proje...
Method 1: Use the target event attribute of the E...
This article describes how to install opencv with...
Since the project requires a questionnaire, but th...
location expression type ~ indicates to perform a...