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
Table of contents JavaScript function call classi...
Table of contents 1. v-text text rendering instru...
What if you designers want to use the font below ...
First download the compressed version of mysql, t...
This article describes the MySQL multi-table join...
Table of contents Prerequisites RN passes value t...
1. First introduce several commonly used MySQL fu...
Add multiple lines to the specified file in Docke...
Now, more and more front-end developers are starti...
Table of contents Lazy Loading CSS styles: HTML p...
First, perform a simple Docker installation. To c...
Recently, students from the User Experience Team o...
Table of contents Overview What is Image Compress...
Table of contents 1. Effect diagram (multiple col...
I often see some circular wave graphics on mobile...