mysql多对多搜索
作者:互联网
我有下表
CREATE TABLE IF NOT EXISTS `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ;
CREATE TABLE IF NOT EXISTS `skill` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ;
CREATE TABLE IF NOT EXISTS `user_skill` (
`user_id` int(11) NOT NULL,
`skill_id` int(11) NOT NULL ,
PRIMARY KEY (`user_id`,`skill_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ;
表user_skill充当多对多表.现在,我想选择所有具有skill_ids至少1&的用户. 2.如何在mysql中做到这一点
解决方法:
select u.id, u.name
from `user` u
inner join `user_skill` us on u.id = us.user_id
where us.skill_id in (1, 2)
group by u.id
having count(distinct us.skill_id) = 2
标签:many-to-many,mysql 来源: https://codeday.me/bug/20191127/2075787.html