这里有 user表、teacher表,其中 teacher.user_ids 字段中的值是 user.id 值以英文半角逗号拼接而来。
现在, 我们需要在查询 teacher 表数据时,将 user.name 的值也查询出来。
使用以下的SQL语句,即可实现需求。
SELECT
GROUP_CONCAT(DISTINCT u.name) AS nameList,
t.user_ids,
t.*
FROM teacher t
LEFT JOIN `user` u ON FIND_IN_SET(u.id, t.user_ids) > 0 AND u.is_deleted = 'n'
GROUP BY t.teacher_id
LIMIT 100;
在以上的SQL中,需要注意的是:FIND_IN_SET(u.id, t.user_ids) > 0,GROUP BY t.teacher_id 这两组条件不可少。
在以上的SQL中, 我的数据库 sql_mode 中不含 'ONLY_FULL_GROUP_BY' 值 . 不了解 "sql_mode" 含义的小伙伴,可以参考我之前的一篇文章: MySQL的sql_mode模式
使用的数据库表、表数据如下所示。
CREATE TABLE user (
id bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'ID, 主键.',
is_deleted varchar(4) DEFAULT NULL COMMENT '是否删除, n否,y是.',
name varchar(100) DEFAULT NULL COMMENT '姓名',
age int(3) DEFAULT NULL COMMENT '年龄',
create_date datetime DEFAULT NULL COMMENT '创建时间',
PRIMARY KEY (id)
) ENGINE=InnoDB AUTO_INCREMENT=1018 DEFAULT CHARSET=utf8mb4 COMMENT='用户表';
CREATE TABLE teacher (
teacher_id bigint(12) NOT NULL AUTO_INCREMENT COMMENT '主键',
teacher_name varchar(255) DEFAULT '' COMMENT '姓名',
teacher_image varchar(255) DEFAULT '' COMMENT '图片',
teacher_age int(3) DEFAULT NULL COMMENT '年龄',
teachert_address varchar(255) DEFAULT NULL COMMENT '地址',
teacher_sex varchar(8) DEFAULT NULL COMMENT '性别',
teacher_status int(2) DEFAULT NULL COMMENT '状态, 0-正常, 1-异常. ',
create_date datetime DEFAULT NULL COMMENT '创建时间',
update_date datetime DEFAULT NULL COMMENT '修改时间',
user_ids varchar(255) DEFAULT NULL COMMENT 'user表中的ID,以 '','' 分割.',
PRIMARY KEY (teacher_id)
) ENGINE=InnoDB AUTO_INCREMENT=38 DEFAULT CHARSET=utf8 COMMENT='教师表';
-- 用户表 数据
INSERT INTO user(id, is_deleted, name, age, create_date) VALUES (1, 'n', '张三三', 28, '2020-04-02 09:54:23');
INSERT INTO user(id, is_deleted, name, age, create_date) VALUES (2, 'n', '硫萄萄糖', 27, '2020-01-05 11:20:00');
INSERT INTO user(id, is_deleted, name, age, create_date) VALUES (4, 'n', '李四', 25, '2019-10-22 16:27:03');
INSERT INTO user(id, is_deleted, name, age, create_date) VALUES (5, 'n', '刘涛', 22, '2020-04-06 08:07:30');
INSERT INTO user(id, is_deleted, name, age, create_date) VALUES (6, 'n', '张曼玉', 18, '2020-04-09 17:21:45');
INSERT INTO user(id, is_deleted, name, age, create_date) VALUES (7, 'n', '刘德华', 33, '2020-03-18 17:22:07');
INSERT INTO user(id, is_deleted, name, age, create_date) VALUES (8, 'n', '刘备', 23, '2020-03-17 17:22:34');
INSERT INTO user(id, is_deleted, name, age, create_date) VALUES (1001, 'n', '王小二', 18, '2020-04-06 08:07:30');
INSERT INTO user(id, is_deleted, name, age, create_date) VALUES (1005, 'y', '王小二', 18, '2021-06-06 15:14:10');
INSERT INTO user(id, is_deleted, name, age, create_date) VALUES (1006, 'n', 'admin', 20, '2020-02-20 16:12:45');
INSERT INTO user(id, is_deleted, name, age, create_date) VALUES (1017, 'y', 'A4', 20, '2022-11-15 10:52:51');
-- 教师表 数据
INSERT INTO teacher(teacher_id, teacher_name, teacher_image, teacher_age, teachert_address, teacher_sex, teacher_status, create_date, update_date, user_ids) VALUES (1, 'hhh', 'hhh.jpg', NULL, NULL, NULL, 1, '2022-09-20 16:17:25', '2022-09-21 18:17:35', '1,2,4');
INSERT INTO teacher(teacher_id, teacher_name, teacher_image, teacher_age, teachert_address, teacher_sex, teacher_status, create_date, update_date, user_ids) VALUES (2, 'ggg', 'ggg.jpg', NULL, NULL, NULL, 4, NULL, NULL, '1,2,5');
INSERT INTO teacher(teacher_id, teacher_name, teacher_image, teacher_age, teachert_address, teacher_sex, teacher_status, create_date, update_date, user_ids) VALUES (3, 'abc', 'abc.jpg', NULL, NULL, NULL, 3, NULL, NULL, '5,6');
INSERT INTO teacher(teacher_id, teacher_name, teacher_image, teacher_age, teachert_address, teacher_sex, teacher_status, create_date, update_date, user_ids) VALUES (4, 'def', 'def.jpg', NULL, NULL, NULL, 8, '2022-10-21 11:30:46', NULL, '1001,1002');
INSERT INTO teacher(teacher_id, teacher_name, teacher_image, teacher_age, teachert_address, teacher_sex, teacher_status, create_date, update_date, user_ids) VALUES (5, '张江', 'hhh.jpg', 25, '浙江省-杭州市-江干区', '男', 1, '2020-01-15 00:00:00', '2020-02-22 00:00:00', '1001,1005');
INSERT INTO teacher(teacher_id, teacher_name, teacher_image, teacher_age, teachert_address, teacher_sex, teacher_status, create_date, update_date, user_ids) VALUES (6, '柳乐儿', 'ggg.jpg', 24, NULL, '女', 4, NULL, '2021-03-04 00:00:00', '1001,1002');
INSERT INTO teacher(teacher_id, teacher_name, teacher_image, teacher_age, teachert_address, teacher_sex, teacher_status, create_date, update_date, user_ids) VALUES (7, '王大锤', 'abc.jpg', NULL, '浙江省-杭州市-余杭区', NULL, 3, '2020-05-06 00:00:00', NULL, NULL);
INSERT INTO teacher(teacher_id, teacher_name, teacher_image, teacher_age, teachert_address, teacher_sex, teacher_status, create_date, update_date, user_ids) VALUES (11, '张三奶奶', 'hhh.jpg', 90, '辽宁省-营口区-长白山', '女', 1, '1932-08-25 16:34:41', '2022-10-12 18:42:33', NULL);
INSERT INTO teacher(teacher_id, teacher_name, teacher_image, teacher_age, teachert_address, teacher_sex, teacher_status, create_date, update_date, user_ids) VALUES (12, '跑龙套', 'ggg.jpg', 21, '北京市-长江影视基地-5星级豪华酒店', '男', 7, '2012-01-22 10:06:42', '2015-03-26 17:16:06', NULL);
INSERT INTO teacher(teacher_id, teacher_name, teacher_image, teacher_age, teachert_address, teacher_sex, teacher_status, create_date, update_date, user_ids) VALUES (13, '卡卡西', 'abc.jpg', 38, '日本-大阪市-火之国-木叶村', '男', 4, '2012-01-22 10:06:42', '2015-03-26 17:16:06', NULL);
INSERT INTO teacher(teacher_id, teacher_name, teacher_image, teacher_age, teachert_address, teacher_sex, teacher_status, create_date, update_date, user_ids) VALUES (14, 'BoB', 'def.jpg', 42, '美国-纽约市-街道办处105号-204', '男', 5, '2022-03-01 08:05:07', '2022-10-21 11:30:46', NULL);
INSERT INTO teacher(teacher_id, teacher_name, teacher_image, teacher_age, teachert_address, teacher_sex, teacher_status, create_date, update_date, user_ids) VALUES (36, '张三', 'zhangsan.png', 12, '浙江省-杭州市-大李屯', '男', 1, '2022-10-25 17:03:28', '2022-10-25 17:03:28', NULL);
INSERT INTO teacher(teacher_id, teacher_name, teacher_image, teacher_age, teachert_address, teacher_sex, teacher_status, create_date, update_date, user_ids) VALUES (37, '张三2222', 'zhangsan.png', 12, '浙江省-杭州市-大李屯', '男', 1, '2022-10-25 17:04:29', '2022-10-25 17:04:29', NULL);
在和同行前辈交流过程中,前辈讲我的这种写法不常见,使用了内置函数,可能会存在一些坑。这种写法,我刚刚使用,大家在自己的工作中,选择性的使用。
参考文章:
MySql中FIND_IN_SET()函数用法
MySQL中find_in_set()函数用法详解
MYSQL中FIND_IN_SET()函数的使用(转载)