在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
1、简介
字段计算经常会用到如下两种:
2、正文
2.1 字段拼接准备一张user表,插入几条数据,如下所示: 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 '主键', `name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '用户名', `nation` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '民族', 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, '李四', '维吾尔族'); INSERT INTO `user` VALUES (4, '王五', '蒙古族'); SET FOREIGN_KEY_CHECKS = 1; 需求: 获取用户的姓名和民族组合信息 语句: mysql> select concat(name, '(',nation, ')') from user; +---------------------------------+ | concat(name, '(',nation, ')') | +---------------------------------+ | 李子捌(汉族) | | 张三(回族) | | 李四(维吾尔族) | | 王五(蒙古族) | +---------------------------------+ 解析: 这里使用了 关于组合之后的字段名问题? 细心的小伙伴发现组合之后的字段名使用的是 mysql> select concat(name, '(',nation, ')') as user_message from user; +------------------+ | user_message | +------------------+ | 李子捌(汉族) | | 张三(回族) | | 李四(维吾尔族) | | 王五(蒙古族) | +------------------+ 别名的用法就是使用 2.2 字段执行算术计算组合字段我们往往不只是简单的字符串拼接,可能会涉及到字段与字段之间的算术预算,此时我们就需要使用到 MySQL提供了加减乘除操作符如下所示:
准备一张product表,插入几条数据,如下所示: 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 '主键', `product_name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '产品名称', `price` decimal(10, 2) UNSIGNED NOT NULL COMMENT '产品价格', `number` int(11) NOT NULL COMMENT '产品数量', 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; 需求: 查询目前库存产品总值 语句: 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 | +-------------------------+-------------+ 运算符顺序问题:
示例: 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) 值得注意的是 mysql> select concat(12 / 0); +----------------+ | concat(12 / 0) | +----------------+ | NULL | +----------------+ 1 row in set, 1 warning (0.00 sec) 到此这篇关于MySQL中组合字段之 |
请发表评论