CREATE TABLE `ab` (
`model_id` varchar(40) NOT NULL,
`name` varchar(32) COLLATE utf8mb4_general_ci DEFAULT ''
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
insert into ab (model_id,name) VALUES('112,124,13154','大众集合'),('1121,1234,15154','奥迪集合'),('212,224,23154','丰田集合');
CREATE TABLE `ba` (
`id` varchar(40) NOT NULL,
`name` varchar(32) COLLATE utf8mb4_general_ci DEFAULT ''
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
insert into ba (id,name) VALUES('112','大众_1'),('124','大众_2'),('13154','大众_3'),('1121','奥迪_1'),('1234','奥迪_2'),('15154','奥迪_3'),('212','丰田_1'),('224','丰田_2'),('23154','丰田_3');
select *from ab;
select *from ba;
--查询id为112,124,13154在表ba对应的name.
步骤:先在ab表中查询出大众集合的model_id,然后在ba表查询出id对应的name
select id,name from ba where id in (select model_id from ab where name='大众集合');
如果用in去执行 带逗号的字符串你会发现一条都没查询出来。怎么能查询出来呢?
我们用find_in_set(a,b),用a in(b)去理解就行了
select id,name from ba where find_in_set(id,(select model_id from ab where name='大众集合'));