MySql逗号拼接字段拆分查询

遇到的问题

一个服务分类对应多个适用门店,一个门店存在多个服务分类。现需要同时查询多个门店所以有的服务分类。
例:“热门分类”适用于“58cb6557e86b40c3b1e69ad7dc7a8072,cc27487409a24343a1f09924a22cb754,f99f4c93ce224e42bcad0b26d48966da”三个门店,现需要根据“58cb6557e86b40c3b1e69ad7dc7a8072,f99f4c93ce224e42bcad0b26d48966da”查询门店的服务分类。
在这里插入图片描述

MySql结构和数据

MySql逗号拼接字段拆分查询_第1张图片
MySql逗号拼接字段拆分查询_第2张图片

/*
Navicat MySQL Data Transfer

Date: 2019-10-23 11:42:45
*/

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for t_service_category
-- ----------------------------
DROP TABLE IF EXISTS `t_service_category`;
CREATE TABLE `t_service_category` (
  `id` varchar(32) CHARACTER SET utf8mb4 NOT NULL COMMENT '主键id',
  `service_name` varchar(255) DEFAULT '' COMMENT '服务类别名称',
  `remark` varchar(255) DEFAULT NULL COMMENT '备注',
  `service_status` varchar(255) DEFAULT NULL COMMENT '服务类别状态;1,启用;0,未启用',
  `dept_id` varchar(500) DEFAULT NULL COMMENT '适用门店',
  `creater` varchar(255) DEFAULT NULL COMMENT '创建人',
  `creater_time` varchar(255) DEFAULT NULL COMMENT '创建时间',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT COMMENT='服务类别';

-- ----------------------------
-- Records of t_service_category
-- ----------------------------
INSERT INTO `t_service_category` VALUES ('2132efc80f4f4f3e8ec8a9c31abb5324', '测试3', '', '1', 'cc27487409a24343a1f09924a22cb754', '1320fbf46d714c25ab3d3ab381bf7302', '2019-10-10 13:59:00');
INSERT INTO `t_service_category` VALUES ('2347e47427044cc5a890041a9f4a05fb', '测试5', '', '1', 'f99f4c93ce224e42bcad0b26d48966da', '1320fbf46d714c25ab3d3ab381bf7302', '2019-10-09 10:34:38');
INSERT INTO `t_service_category` VALUES ('2825998ce01d4a23a77e4c18df0b0fff', '测试6', '', '1', 'f99f4c93ce224e42bcad0b26d48966da', '1320fbf46d714c25ab3d3ab381bf7302', '2019-10-09 10:34:45');
INSERT INTO `t_service_category` VALUES ('2a8d55e1d41a43dea0d42b3609978cb0', '测试2', '', '1', '58cb6557e86b40c3b1e69ad7dc7a8072', '1320fbf46d714c25ab3d3ab381bf7302', '2019-09-29 16:17:50');
INSERT INTO `t_service_category` VALUES ('39d5fc32b7224ce9814d04ce47cfed04', '热门分类', '热门分类', '1', '58cb6557e86b40c3b1e69ad7dc7a8072,cc27487409a24343a1f09924a22cb754,f99f4c93ce224e42bcad0b26d48966da', '1320fbf46d714c25ab3d3ab381bf7302', '2019-10-22 17:31:43');
INSERT INTO `t_service_category` VALUES ('3aa3981175344b5a8f68b1ab9a603e7f', '测试4', '', '1', 'cc27487409a24343a1f09924a22cb754', '1320fbf46d714c25ab3d3ab381bf7302', '2019-10-10 14:01:10');
INSERT INTO `t_service_category` VALUES ('5fbcfb3f5c23448980eabb224f66f7c2', '测试1', '', '1', '58cb6557e86b40c3b1e69ad7dc7a8072', '1320fbf46d714c25ab3d3ab381bf7302', '2019-09-29 16:17:37');

解决办法

MySql逗号拼接字段拆分查询_第3张图片

SELECT
	s.id,
	s.service_name,
	s.remark,
	s.service_status,
	s.dept_id,
	s.creater,
	s.creater_time
FROM
	t_service_category s
LEFT JOIN mysql.help_topic b ON b.help_topic_id < (
	LENGTH(s.dept_id) - LENGTH(REPLACE(s.dept_id, ',', '')) + 1
)
WHERE
	FIND_IN_SET(
		substring_index(
			substring_index(
				s.dept_id,
				',',
				b.help_topic_id + 1
			),
			',',
			- 1
		),
		'58cb6557e86b40c3b1e69ad7dc7a8072,f99f4c93ce224e42bcad0b26d48966da'
	)
GROUP BY
	s.service_name
ORDER BY
	s.creater_time DESC
参考链接

https://cloud.tencent.com/developer/article/1472131

你可能感兴趣的:(SQL相关,逗号拆分查询,逗号拼接查询)