【Mysql】substring_index 函数详解 命令格式 string substring_index(string , string , int ) 命令说明 截取字符串str第count
string substring_index(string <str>, string <separator>, int <count>)
截取字符串str第count个分隔符之前的字符串。如果count为正,则从左边开始截取。如果count为负,则从右边开始截取。此函数为MaxCompute 2.0扩展函数。
返回STRING
类型。如果任一输入参数值为NULL,返回NULL。
# 返回 Https://help.codinGCeselect substring_index('https://help.codingce.com', '.', 2);# 返回 codingce.comselect substring_index('https://help.codingce.com', '.', -2);
# 返回NULLselect substring_index('https://help.codingce.com', null, 2);
假设有三个 IP:127.0.0.1、192.128.0.15、255.255.255.255,要分别取每一个号段的值并返回。
SET NAMES utf8mb4;SET FOREIGN_KEY_CHECKS = 0;-- ------------------------------ Table structure for tb_ip-- ----------------------------DROP TABLE IF EXISTS `tb_ip`;CREATE TABLE `tb_ip` ( `pid` int(11) NOT NULL AUTO_INCREMENT, `ip` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, PRIMARY KEY (`pid`) USING BTREE) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;-- ------------------------------ Records of tb_ip-- ----------------------------INSERT INTO `tb_ip` VALUES (1, '127.0.0.1');INSERT INTO `tb_ip` VALUES (2, '192.128.0.15');INSERT INTO `tb_ip` VALUES (3, '255.255.255.255');SET FOREIGN_KEY_CHECKS = 1;
SELECTip,SUBSTRING_INDEX( tb_ip.ip, '.', 1 ) AS part1,SUBSTRING_INDEX( SUBSTRING_INDEX( tb_ip.ip, '.', 2 ), '.', -1) AS part2,SUBSTRING_INDEX( SUBSTRING_INDEX( tb_ip.ip, '.', 3 ), '.', -1) AS part3,SUBSTRING_INDEX( SUBSTRING_INDEX( tb_ip.ip, '.', 4 ), '.', -1) AS part4FROMtb_ip;
结果:
ippart1part2part3part4127.0.0.1127001192.128.0.15192128015255.255.255.255255255255255
可以说是一个面试题,解法有多种,那么如果用 SUBSTRING_INDEX
要如何编写呢?
SET NAMES utf8mb4;SET FOREIGN_KEY_CHECKS = 0;-- ------------------------------ Table structure for tb_value_output-- ----------------------------DROP TABLE IF EXISTS `tb_value_output`;CREATE TABLE `tb_value_output` ( `pid` int(11) NOT NULL AUTO_INCREMENT, `year` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, `month` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, `amount` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, PRIMARY KEY (`pid`) USING BTREE) ENGINE = InnoDB AUTO_INCREMENT = 9 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;-- ------------------------------ Records of tb_value_output-- ----------------------------INSERT INTO `tb_value_output` VALUES (1, '1991', '1', '1.1');INSERT INTO `tb_value_output` VALUES (2, '1991', '2', '1.2');INSERT INTO `tb_value_output` VALUES (3, '1991', '3', '2.2');INSERT INTO `tb_value_output` VALUES (4, '1991', '4', '2.5');INSERT INTO `tb_value_output` VALUES (5, '1992', '1', '2.1');INSERT INTO `tb_value_output` VALUES (6, '1992', '2', '2.4');INSERT INTO `tb_value_output` VALUES (7, '1992', '3', '2.1');INSERT INTO `tb_value_output` VALUES (8, '1992', '4', '2.5');SET FOREIGN_KEY_CHECKS = 1;
SELECTvo.year,SUBSTRING_INDEX( GROUP_CONCAT( vo.amount ), ",", 1 ) AS m1,SUBSTRING_INDEX( SUBSTRING_INDEX( GROUP_CONCAT( vo.amount ), ",", 2 ), ",", - 1 ) AS m2,SUBSTRING_INDEX( SUBSTRING_INDEX( GROUP_CONCAT( vo.amount ), ",", - 2 ), ",", 1 ) AS m3,SUBSTRING_INDEX( GROUP_CONCAT( vo.amount ), ",", - 1 ) AS m4 FROMtb_value_output vo GROUP BYvo.year
结果:
yearm1m2m3m419911.11.22.22.519922.12.42.12.5
GROUP_CONCAT 函数:将相同的行组合起来。
来源地址:https://blog.csdn.net/weixin_43874301/article/details/129516956
--结束END--
本文标题: 【MySQL】substring_index 函数详解
本文链接: https://lsjlt.com/news/421773.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-10-23
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0